cli11
Loading...
Searching...
No Matches
split.cpp File Reference
module cli11

Decomposition of command-line arguments and name specifications. More...

import cli11:string_tools;
import std;

Classes

struct  cli::detail::split_result_t
 One argument pulled apart into a name and the text following it. More...
struct  cli::detail::option_names_t
 The names extracted from a name specification, sorted by kind. More...

Functions

auto cli::detail::split_short (const std::string &current) -> std::optional< split_result_t >
 Splits a short-form argument such as -fvalue.
auto cli::detail::split_long (const std::string &current) -> std::optional< split_result_t >
 Splits a long-form argument such as --file=value.
auto cli::detail::split_windows_style (const std::string &current) -> std::optional< split_result_t >
 Splits a Windows-style argument such as /file:value.
auto cli::detail::split_names (std::string current) -> std::vector< std::string >
 Splits a comma-separated name specification, trimming each entry.
auto cli::detail::get_default_flag_values (const std::string &str) -> std::vector< std::pair< std::string, std::string > >
 Extracts the default values written into a flag specification.
auto cli::detail::get_names (const std::vector< std::string > &input, bool allow_non_standard=false) -> option_names_t
 Sorts a name specification into short, long, and positional names.

Detailed Description

Decomposition of command-line arguments and name specifications.

Two separate jobs live here. The split_* functions take one argument off the command line and pull it apart into a name and a value. The get_names and get_default_flag_values functions take a name specification as written by the caller — something like "-f,--file,filename" — and work out what was meant.

Function Documentation

◆ get_default_flag_values()

auto cli::detail::get_default_flag_values ( const std::string & str) -> std::vector< std::pair< std::string, std::string > >
nodiscardexport

Extracts the default values written into a flag specification.

Recognises name{value} for an explicit default and a leading ! for a negated flag, which defaults to "false". Entries carrying neither are skipped.

Parameters
strThe flag specification, for example "--flag{7},!--no-flag".
Returns
Each flag name paired with its default value.

◆ get_names()

auto cli::detail::get_names ( const std::vector< std::string > & input,
bool allow_non_standard = false ) -> option_names_t
nodiscardexport

Sorts a name specification into short, long, and positional names.

Parameters
inputThe individual names, as produced by split_names.
allow_non_standardAccept multi-character short names such as -abc.
Returns
The names, sorted by kind.
Exceptions
cli::bad_name_string_tIf a name is malformed, reserved, or if more than one positional name is given.

◆ split_long()

auto cli::detail::split_long ( const std::string & current) -> std::optional< split_result_t >
nodiscardexport

Splits a long-form argument such as --file=value.

Parameters
currentThe argument to split.
Returns
The name and the value after =, or std::nullopt if current is not a long-form argument. The value is empty when there is no =.

◆ split_names()

auto cli::detail::split_names ( std::string current) -> std::vector< std::string >
nodiscardexport

Splits a comma-separated name specification, trimming each entry.

Parameters
currentThe specification to split.
Returns
The individual names, each trimmed of surrounding whitespace.

◆ split_short()

auto cli::detail::split_short ( const std::string & current) -> std::optional< split_result_t >
nodiscardexport

Splits a short-form argument such as -fvalue.

Parameters
currentThe argument to split.
Returns
The single-character name and the remainder of the argument, or std::nullopt if current is not a short-form argument.

◆ split_windows_style()

auto cli::detail::split_windows_style ( const std::string & current) -> std::optional< split_result_t >
nodiscardexport

Splits a Windows-style argument such as /file:value.

Parameters
currentThe argument to split.
Returns
The name and the value after :, or std::nullopt if current is not a Windows-style argument. The value is empty when there is no :.