Header lexy/input/argv_input.hpp

An input that uses the command line arguments.

Range lexy::argv_begin/lexy::argv_end

lexy/input/argv_input.hpp
namespace lexy
{
    class argv_sentinel;
    class argv_iterator;

    constexpr argv_iterator argv_begin(int argc, char argv[]) noexcept;
    constexpr argv_iterator argv_end(int argc, char argv[]) noexcept;
}

An iterator range over the characters of the command-line arguments.

argv_iterator is a bidirectional iterator with value type const char that iterates over the characters of the command-line arguments. It includes the null character as separator between two arguments, but not after the last argument. argv_sentinel is its corresponding sentinel.

argv_begin returns an iterator to the first character of the first command-line argument after the executable name (i.e. argv[1][0]), argv_end returns an iterator one past the last character of the last argument (i.e. argv[argc - 1] + std::strlen(argv[argc - 1])).

Example 1. Print the command-line arguments
int main(int argc, char* argv[])
{
    auto begin = lexy::argv_begin(argc, argv);
    auto end   = lexy::argv_end(argc, argv);

    for (auto cur = begin; cur != end; ++cur)
    {
        if (*cur == '\0')
            std::puts("\\0");
        else
            std::printf("%c", *cur);
    }
    std::puts("\\0"); // last null terminator not included in the range
}

Input lexy::argv_input

lexy/input/argv_input.hpp
namespace lexy
{
    template <encoding Encoding = default_encoding>
    class argv_input
    {
    public:
        using encoding  = Encoding;
        using char_type = typename encoding::char_type;

        constexpr argv_input();

        constexpr argv_input(argv_iterator begin, argv_iterator end) noexcept;
        constexpr argv_input(int argc, char* argv[]) noexcept
        : argv_input(argv_begin(argc, argv), argv_end(argc, argv))
        {}

        constexpr reader auto reader() const& noexcept;
    };
}

The class argv_input is an input that uses the command-line arguments as input.

Its encoding must have char as primary or secondary character type. The range [begin, end) of the lexy::argv_iterator  will be used as input.

Example 2. Use the command-line arguments as input
int main(int argc, char* argv[])
{
    // Create the input.
    lexy::argv_input input(argc, argv);

    // Use the input.
    if (!lexy::match<production>(input))
    {
        std::puts("Error!\n");
        return 1;
    }
}
Note
The input will contain \0 as separator between two command-line arguments. Use the rule lexy::dsl::argv_separator  to match it.

Convenience typedefs

lexy/input/argv_input.hpp
namespace lexy
{
    template <encoding Encoding = default_encoding>
    using argv_lexeme = lexeme_for<argv_input<Encoding>>;

    template <typename Tag, encoding Encoding = default_encoding>
    using argv_error = error_for<argv_input<Encoding>, Tag>;

    template <encoding Encoding = default_encoding>
    using argv_error_context = error_context<argv_input<Encoding>>;
}

Convenience typedefs for the command-line argument input.

Token rule lexy::dsl::argv_separator

lexy/input/argv_input.hpp
namespace lexy::dsl
{
    constexpr token-rule auto argv_separator;
}

argv_separator is a token rule that matches the separator between two command-line arguments.

Matching

Matches and consumes the separator between two command-line arguments.

Errors

lexy::expected_char_class  ("argv-separator"): at the unchanged reader position. The rule then fails.

Parse tree

Single token node with the lexy::predefined_token_kind  lexy::literal_token_kind.

The rule can only succeed on a lexy::argv_input . There is no argument separator before the first command-line argument, or after the last one.

See also