Remove trailing spaces from README lines

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
This commit is contained in:
Sean Robinson 2021-04-05 10:34:32 -07:00
parent e371313b87
commit a711f26045

View File

@ -24,7 +24,7 @@ Simply include argparse.hpp and you're good to go.
#include <argparse/argparse.hpp> #include <argparse/argparse.hpp>
``` ```
To start parsing command-line arguments, create an ```ArgumentParser```. To start parsing command-line arguments, create an ```ArgumentParser```.
```cpp ```cpp
argparse::ArgumentParser program("program name"); argparse::ArgumentParser program("program name");
@ -63,7 +63,7 @@ int main(int argc, char *argv[]) {
std::cout << program; std::cout << program;
exit(0); exit(0);
} }
auto input = program.get<int>("square"); auto input = program.get<int>("square");
std::cout << (input * input) << std::endl; std::cout << (input * input) << std::endl;
@ -82,11 +82,11 @@ Here's what's happening:
* The ```add_argument()``` method is used to specify which command-line options the program is willing to accept. In this case, Ive named it square so that its in line with its function. * The ```add_argument()``` method is used to specify which command-line options the program is willing to accept. In this case, Ive named it square so that its in line with its function.
* Command-line arguments are strings. Inorder to square the argument and print the result, we need to convert this argument to a number. In order to do this, we use the ```.action``` method and provide a lambda function that tries to convert user input into an integer. * Command-line arguments are strings. Inorder to square the argument and print the result, we need to convert this argument to a number. In order to do this, we use the ```.action``` method and provide a lambda function that tries to convert user input into an integer.
* We can get the value stored by the parser for a given argument using ```parser.get<T>(key)``` method. * We can get the value stored by the parser for a given argument using ```parser.get<T>(key)``` method.
### Optional Arguments ### Optional Arguments
Now, let's look at ***optional arguments***. Optional arguments start with ```-``` or ```--```, e.g., ```--verbose``` or ```-a```. Optional arguments can be placed anywhere in the input sequence. Now, let's look at ***optional arguments***. Optional arguments start with ```-``` or ```--```, e.g., ```--verbose``` or ```-a```. Optional arguments can be placed anywhere in the input sequence.
```cpp ```cpp
@ -116,10 +116,10 @@ $ ./main --verbose
Verbosity enabled Verbosity enabled
``` ```
Here's what's happening: Here's what's happening:
* The program is written so as to display something when --verbose is specified and display nothing when not. * The program is written so as to display something when --verbose is specified and display nothing when not.
* Since the argument is actually optional, no error is thrown when running the program without ```--verbose```. Note that by using ```.default_value(false)```, if the optional argument isnt used, it's value is automatically set to false. * Since the argument is actually optional, no error is thrown when running the program without ```--verbose```. Note that by using ```.default_value(false)```, if the optional argument isnt used, it's value is automatically set to false.
* By using ```.implicit_value(true)```, the user specifies that this option is more of a flag than something that requires a value. When the user provides the --verbose option, it's value is set to true. * By using ```.implicit_value(true)```, the user specifies that this option is more of a flag than something that requires a value. When the user provides the --verbose option, it's value is set to true.
#### Requiring optional arguments #### Requiring optional arguments
@ -131,7 +131,7 @@ program.add_argument("-o", "--output")
.help("specify the output file."); .help("specify the output file.");
``` ```
If the user does not provide a value for this parameter, an exception is thrown. If the user does not provide a value for this parameter, an exception is thrown.
Alternatively, you could provide a default value like so: Alternatively, you could provide a default value like so:
@ -164,7 +164,7 @@ argparse::ArgumentParser program;
program.add_argument("integer") program.add_argument("integer")
.help("Input number") .help("Input number")
.action([](const std::string& value) { return std::stoi(value); }); .action([](const std::string& value) { return std::stoi(value); });
program.add_argument("floats") program.add_argument("floats")
.help("Vector of floats") .help("Vector of floats")
.nargs(4) .nargs(4)
@ -188,7 +188,7 @@ integer : -5
floats : -1.1 -3.1415 -310 -4513.29 floats : -1.1 -3.1415 -310 -4513.29
``` ```
As you can see here, ```argparse``` supports negative integers, negative floats and scientific notation. As you can see here, ```argparse``` supports negative integers, negative floats and scientific notation.
### Combining Positional and Optional Arguments ### Combining Positional and Optional Arguments
@ -274,7 +274,7 @@ catch (const std::runtime_error& err) {
auto files = program.get<std::vector<std::string>>("--input_files"); // {"config.yml", "System.xml"} auto files = program.get<std::vector<std::string>>("--input_files"); // {"config.yml", "System.xml"}
``` ```
```ArgumentParser.get<T>()``` has specializations for ```std::vector``` and ```std::list```. So, the following variant, ```.get<std::list>```, will also work. ```ArgumentParser.get<T>()``` has specializations for ```std::vector``` and ```std::list```. So, the following variant, ```.get<std::list>```, will also work.
```cpp ```cpp
auto files = program.get<std::list<std::string>>("--input_files"); // {"config.yml", "System.xml"} auto files = program.get<std::list<std::string>>("--input_files"); // {"config.yml", "System.xml"}
@ -352,10 +352,10 @@ c = {0.0, 0.0}
``` ```
Here's what's happening: Here's what's happening:
* We have three optional arguments ```-a```, ```-b``` and ```-c```. * We have three optional arguments ```-a```, ```-b``` and ```-c```.
* ```-a``` and ```-b``` are toggle arguments. * ```-a``` and ```-b``` are toggle arguments.
* ```-c``` requires 2 floating point numbers from the command-line. * ```-c``` requires 2 floating point numbers from the command-line.
* argparse can handle compound arguments, e.g., ```-abc``` or ```-bac``` or ```-cab```. This only works with short single-character argument names. * argparse can handle compound arguments, e.g., ```-abc``` or ```-bac``` or ```-cab```. This only works with short single-character argument names.
- ```-a``` and ```-b``` become true. - ```-a``` and ```-b``` become true.
- argv is further parsed to identify the inputs mapped to ```-c```. - argv is further parsed to identify the inputs mapped to ```-c```.
- If argparse cannot find any arguments to map to c, then c defaults to {0.0, 0.0} as defined by ```.default_value``` - If argparse cannot find any arguments to map to c, then c defaults to {0.0, 0.0} as defined by ```.default_value```
@ -371,28 +371,28 @@ $ compiler file1 file2 file3
To enable this, simply create an argument and mark it as `remaining`. All remaining arguments passed to argparse are gathered here. To enable this, simply create an argument and mark it as `remaining`. All remaining arguments passed to argparse are gathered here.
```cpp ```cpp
argparse::ArgumentParser program("compiler"); argparse::ArgumentParser program("compiler");
program.add_argument("files") program.add_argument("files")
.remaining(); .remaining();
try { try {
program.parse_args(argc, argv); program.parse_args(argc, argv);
} }
catch (const std::runtime_error& err) { catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl; std::cout << err.what() << std::endl;
std::cout << program; std::cout << program;
exit(0); exit(0);
} }
try { try {
auto files = program.get<std::vector<std::string>>("files"); auto files = program.get<std::vector<std::string>>("files");
std::cout << files.size() << " files provided" << std::endl; std::cout << files.size() << " files provided" << std::endl;
for (auto& file : files) for (auto& file : files)
std::cout << file << std::endl; std::cout << file << std::endl;
} catch (std::logic_error& e) { } catch (std::logic_error& e) {
std::cout << "No files provided" << std::endl; std::cout << "No files provided" << std::endl;
} }
``` ```
When no arguments are provided: When no arguments are provided:
@ -415,21 +415,21 @@ baz.txt
The process of gathering remaining arguments plays nicely with optional arguments too: The process of gathering remaining arguments plays nicely with optional arguments too:
```cpp ```cpp
argparse::ArgumentParser program("compiler"); argparse::ArgumentParser program("compiler");
program.add_arguments("-o") program.add_arguments("-o")
.default_value(std::string("a.out")); .default_value(std::string("a.out"));
program.add_argument("files") program.add_argument("files")
.remaining(); .remaining();
try { try {
program.parse_args(argc, argv); program.parse_args(argc, argv);
} }
catch (const std::runtime_error& err) { catch (const std::runtime_error& err) {
std::cout << err.what() << std::endl; std::cout << err.what() << std::endl;
std::cout << program; std::cout << program;
exit(0); exit(0);
} }
auto output_filename = program.get<std::string>("-o"); auto output_filename = program.get<std::string>("-o");
@ -437,8 +437,8 @@ std::cout << "Output filename: " << output_filename << std::endl;
try { try {
auto files = program.get<std::vector<std::string>>("files"); auto files = program.get<std::vector<std::string>>("files");
std::cout << files.size() << " files provided" << std::endl; std::cout << files.size() << " files provided" << std::endl;
for (auto& file : files) for (auto& file : files)
std::cout << file << std::endl; std::cout << file << std::endl;
} catch (std::logic_error& e) { } catch (std::logic_error& e) {
std::cout << "No files provided" << std::endl; std::cout << "No files provided" << std::endl;