Added prefix_chars and assign_chars to README #67

This commit is contained in:
Pranav Srinivas Kumar 2022-09-21 09:53:30 -07:00
parent 66730967aa
commit 632ca2fcf8

View File

@ -39,6 +39,7 @@
* [Subcommands](#subcommands)
* [Parse Known Args](#parse-known-args)
* [Custom Prefix Characters](#custom-prefix-characters)
* [Custom Assignment Characters](#custom-assignment-characters)
* [Further Examples](#further-examples)
* [Construct a JSON object from a filename argument](#construct-a-json-object-from-a-filename-argument)
* [Positional Arguments with Compound Toggle Arguments](#positional-arguments-with-compound-toggle-arguments)
@ -826,7 +827,9 @@ int main(int argc, char *argv[]) {
### Custom Prefix Characters
Most command-line options will use `-` as the prefix, e.g. `-f/--foo`. Parsers that need to support different or additional prefix characters, e.g. for options like `+f` or `/foo`, may specify them using the `set_prefix_chars()`:
Most command-line options will use `-` as the prefix, e.g. `-f/--foo`. Parsers that need to support different or additional prefix characters, e.g. for options like `+f` or `/foo`, may specify them using the `set_prefix_chars()`.
The default prefix character is `-`.
```cpp
#include <argparse/argparse.hpp>
@ -870,6 +873,49 @@ foo@bar:/home/dev/$ ./main +f 5 --bar 3.14f /foo "Hello"
/foo : Hello
```
### Custom Assignment Characters
In addition to prefix characters, custom 'assign' characters can be set. This setting is used to allow invocations like `./test --foo=Foo /B:Bar`.
The default assign character is `=`.
```cpp
#include <argparse/argparse.hpp>
#include <cassert>
int main(int argc, char *argv[]) {
argparse::ArgumentParser program("test");
program.set_prefix_chars("-+/");
program.set_assign_chars("=:");
program.add_argument("--foo");
program.add_argument("/B");
try {
program.parse_args(argc, argv);
}
catch (const std::runtime_error& err) {
std::cerr << err.what() << std::endl;
std::cerr << program;
std::exit(1);
}
if (program.is_used("--foo")) {
std::cout << "--foo : " << program.get("--foo") << "\n";
}
if (program.is_used("/B")) {
std::cout << "/B : " << program.get("/B") << "\n";
}
}
```
```console
foo@bar:/home/dev/$ ./main --foo=Foo /B:Bar
--foo : Foo
/B : Bar
```
## Further Examples
### Construct a JSON object from a filename argument