Skip to content

Commit

Permalink
YT-CPPAP-8: Fix typos and rename some elements
Browse files Browse the repository at this point in the history
- Fixed some typos and incorrect examples in the readme file
- Renamed and refactored the `ap::action::check_file_exists_action()` function
  • Loading branch information
SpectraL519 committed Apr 23, 2024
1 parent 60dd035 commit 44a3c8d
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 242 deletions.
63 changes: 32 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ Command-line argument parser for C++20

## Overview

The `CPP-AP` library has been developed for the *Team Programming* course at the *Wrocław University of Science and Technology*.

Faculty: *W04N - Faculty of Information and Communication Technology*

Field of study: *Algorithmic Computer Science*

<br />

The goal of the project was to create a light, intuitive and simple to use command-line argument parser library for the `C++20` and newer standards.

The `CPP-AP` library does not require installing any additional tools or heavy libraries, like with `boost::program_options`. Much like with the `Doctest` framework - the only thing you need to do is copy the `argument_parser.hpp` file into the include directory of your project and you're set to go.

> **NOTE:** [v1.0](https://github.com/SpectraL519/cpp-ap/commit/9a9e5360766b732f322ae2efe3cf5ec5f9268eef) of the library has been developed for the *Team Programming* course at the *Wrocław University of Science and Technology*.
>
> Faculty: *W04N - Faculty of Information and Communication Technology*
>
> Field of study: *Algorithmic Computer Science*
>
> The project has received the 1st place at the 2024 CreatiWITy competition organized by the faculty. The article in Polish can be found on the [faculty website](https://wit.pwr.edu.pl/aktualnosci/oto-laureaci-konkursu-creatiwity-273.html). Please note that this is not a technical article :)
<br />
<br />

Expand Down Expand Up @@ -79,18 +79,18 @@ The parser supports both positional and optional arguments. Both argument types
To add an argument to the parameter's configurations use the following syntax:

```c++
parser.add_<positional/optional>_argument<value_type>("argument_name");
parser.add_<positional/optional>_argument<value_type>("argument");
```

or

```c++
parser.add_<positional/optional>_argument<value_type>("argument_name", "a");
parser.add_<positional/optional>_argument<value_type>("argument", "a");
```

**NOTE:** The library supports any argument value types which meet the following requirements:
* The `std::ostream& operator<<` must be overloaded for a value type
* The type must have a copy constructor and an assignment operator
* The `std::ostream& operator<<` is overloaded for the value type
* The value type has a copy constructor and an assignment operator

**NOTE:** If the `value_type` is not provided, `std::string` will be used.

Expand Down Expand Up @@ -160,10 +160,10 @@ Parameters which can be specified for both positional and optional arguments inc
.action<ap::valued_action>([](const double& value) { return 1. / value; });
```

Actions can also be used to perform some value checking logic, e.g. the predefined `check_file_exists_action` which checks if a file with a given name exists:
Actions can also be used to perform some value checking logic, e.g. the predefined `check_file_exists` which checks if a file with a given name exists:
```c++
parser.add_optional_argument("input", "i")
.action<ap::void_action>(ap::action::check_file_exists_action);
.action<ap::void_action>(ap::action::check_file_exists());
```

**Optional argument specific parameters**
Expand Down Expand Up @@ -231,7 +231,7 @@ The supported default arguments are:
```c++
// equivalent to:
parser.add_positional_argument<std::string>("input")
.action<ap::void_action>(ap::action::check_file_exists_action)
.action<ap::void_action>(ap::action::check_file_exists())
.help("Input file path");
```

Expand All @@ -247,29 +247,30 @@ The supported default arguments are:
parser.add_flag("help", "h").bypass_required().help("Display help message");
```

**Note:** As of now the *on flag action* functionality is not implemented in the library - this will be added in a future release.
To properly use the help argument in the current release add the following right beneath the `parser.parse_args(argc, argv)` try-catch block:
```c++
if (parser.has_value("help")) {
std::cout << parser << std::endl;
std::exit(EXIT_SUCCESS);
}
```
> [!NOTE]
> As of now the *on flag action* functionality is not implemented in the library - this will be added in a future release.
> To properly use the help argument in the current release add the following right beneath the `parser.parse_args(argc, argv)` try-catch block:
> ```c++
> if (parser.has_value("help")) {
> std::cout << parser << std::endl;
> std::exit(EXIT_SUCCESS);
> }
> ```

* `optional::input` and `optional::multi_input`:
```c++
// input - equivalent to:
parser.add_optional_argument("input", "i")
.required()
.nargs(1)
.action<ap::void_action>(ap::action::check_file_exists_action)
.action<ap::void_action>(ap::action::check_file_exists())
.help("Input file path");

// multi_input - equivalent to:
parser.add_optional_argument("input", "i")
.required()
.nargs(ap::nargs::at_least(1))
.action<ap::void_action>(ap::action::check_file_exists_action)
.action<ap::void_action>(ap::action::check_file_exists())
.help("Input files paths");
```

Expand Down Expand Up @@ -298,14 +299,14 @@ To parse the command-line arguments use the `argument_parser::parse_args` method
// power.cpp
#include <ap/argument_parser.hpp>

#include <iostream>
#include <cmath>
#include <iostream>

int main(int argc, char* argv[]) {
// create the parser class instance
ap::argument_parser parser;
parser.program_name("power calculator")
.program_description("calculates the value of an expression: base & exponent");
.program_description("calculates the value of an expression: base ^ exponent");

// add arguments
parser.add_positional_argument<double>("base").help("the exponentation base value");
Expand Down Expand Up @@ -363,11 +364,11 @@ int main(int argc, char* argv[]) {

**NOTE:** For each positional argument there must be **exactly one value**.
```shell
./test_program
./power
# out:
# [ERROR] : No values parsed for a required argument [base]
# power calculator
# calculates the value of an expression: base & exponent
# calculates the value of an expression: base ^ exponent
# [base] : the exponentation base value
# [exponent,e] : the exponent value
# [help,h] : Display help message
Expand Down Expand Up @@ -416,7 +417,7 @@ cd <project-root>/example
The examples' source files are in the `<project-root>/example/source` directory.

> **Note:** Each source file is a sepparate example.
> [!NOTE] Each source file is a sepparate example.

Building the examples:

Expand Down Expand Up @@ -479,7 +480,7 @@ Run the tests:
./test -ts="<test-suite-name>"
```

> **Note**: Test suites in the project have the same name as the files they're in.
> [!NOTE] Test suites in the project have the same names as the files they're in.
<br />
Expand Down
3 changes: 2 additions & 1 deletion change_log.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
* Aligned the `.clang-format` configuration file
* Added the `install_clang17_toolchain.sh` env script
* Added the `format` workflow
* Switched from the `<algorithm>` to the `<ranges>` library for all current container operations
* Switched to the `std::ranges` and `std::views` algorithms for all current container operations
* Modified the `argument_name` structure - renamed members: `name` to `primary`, `short_name` to `secondary`
* Added `argument_name::match(string_view)` and `argument_name::match(argument_name)` functions
* Added aliases for default argument enum classes:
* `ap::default_argument::positional` = `ap::default_posarg`
* `ap::default_argument::optional` = `ap::default_optarg`
* Renamed the predefined: `ap::action::check_file_exists_action` -> `ap::action::check_file_exists`
Loading

0 comments on commit 44a3c8d

Please sign in to comment.