ChainCLI
A modern C++20 command-line interface library
Loading...
Searching...
No Matches
parser.h
1/*
2 * Copyright 2025 Dominik Czekai
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18#include <memory>
19#include <string>
20#include <vector>
21
22#include "cli_config.h"
23#include "cli_context.h"
24#include "commands/command.h"
25#include "context_builder.h"
26
27namespace cli::parsing
28{
31class Parser
32{
33public:
36 explicit Parser(const CliConfig &config) : configuration(config) {}
37
43 void parseArguments(const cli::commands::Command &command,
44 const std::vector<std::string> &inputs,
45 ContextBuilder &contextBuilder) const;
46
47private:
48 std::vector<std::any> parseRepeatableList(const cli::commands::TypedArgumentBase &arg,
49 const std::string &input) const;
50
51 void parseRepeatable(const cli::commands::OptionArgumentBase &arg, const std::string &input,
53
54 void parseRepeatable(const cli::commands::PositionalArgumentBase &arg, const std::string &input,
56
57 void checkGroupsAndRequired(const cli::commands::Command &command,
58 const ContextBuilder &contextBuilder) const;
59
60 bool tryOptionArg(
61 const std::vector<std::shared_ptr<cli::commands::OptionArgumentBase>> &optionArguments,
62 const std::vector<std::string> &inputs, const std::string &currentParsing, size_t index,
63 ContextBuilder &contextBuilder) const;
64
65 bool tryFlagArg(const std::vector<std::shared_ptr<cli::commands::FlagArgument>> &flagArguments,
66 const std::string &currentParsing, ContextBuilder &contextBuilder) const;
67
68 const CliConfig &configuration;
69};
70} // namespace cli::parsing
Builder for CliContext objects, allowing to incrementally add arguments before constructing the final...
Definition context_builder.h:33
Represents a command in the CLI application.
Definition command.h:36
Untemplated base class for option arguments in the CLI. Used to store all option arguments in a singl...
Definition option_argument.h:33
Untemplated Base class for positional arguments in the CLI. Used to store all positional arguments in...
Definition positional_argument.h:34
Base class for typed command-line arguments.
Definition argument.h:108
Class used to parse string inputs into typed argument values based on the command definition.
Definition parser.h:32
Parser(const CliConfig &config)
Creates a new Parser instance with the given configuration.
Definition parser.h:36
void parseArguments(const cli::commands::Command &command, const std::vector< std::string > &inputs, ContextBuilder &contextBuilder) const
Parse the given inputs according to the specified command and populate the context builder with the p...
Definition parser.cpp:136
Holds the configuration for the CLI application.
Definition cli_config.h:24