Skip to content

Commit

Permalink
Add support for @answer-files
Browse files Browse the repository at this point in the history
  • Loading branch information
mzdun committed Oct 17, 2020
1 parent 030ad7a commit 7d74bee
Show file tree
Hide file tree
Showing 12 changed files with 494 additions and 201 deletions.
11 changes: 7 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
*.out
*.app

build
build-*
*-build-*
.idea
/build
/build-*
/*-build-*
/.idea
/.vscode

web.log
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.12)
project (args
VERSION 0.11.0
VERSION 0.12.0
DESCRIPTION ""
LANGUAGES CXX)

Expand Down
44 changes: 39 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Small open-source library for program argument parser, inspired by Python's `argparse`, depending only on the standard library, with C++17 as minimum requirement.

It automatically builds and prints out help info for program arguments, can react to missing or unrecognized arguments and has support for arguments with and without values.
It automatically builds and prints out help info for program arguments, can react to missing or unrecognized arguments and has support for arguments with and without values. It can also support placing long lists of arguments into an [@answer-file](#parseruse_answer_file).

The argument values can be stored in:
- `std::string`s,
Expand Down Expand Up @@ -129,8 +129,6 @@ Short arguments can also be glued together, except that any argument with a valu
-czfvalue
```



# Classes

## args::arglist
Expand Down Expand Up @@ -180,6 +178,7 @@ Produces built in translations for given string ids:
|`needed_enum_known_values`|`"known values for $arg1: $arg2"`|
|`required`|`"argument $arg1 is required"`|
|`error_msg`|`"$arg1: error: $arg2"`|
|`file_not_found`|`"cannot open $arg1"`|

## args::enum_traits<Enum>

Expand Down Expand Up @@ -376,11 +375,46 @@ Parses the arguments. The `on_unknown` instructs the parser, how to react on an
The `maybe_width` is a hint for terminal width. If missing, will try to get the current width of a terminal. If the program is not attached to terminal, will print the messages without any formatting.
### parser::use_answer_file
```cxx
void use_answer_file(char marker = '@');
bool uses_answer_file() const noexcept;
char answer_file_marker() const noexcept;
```

Turns on/off support for answer files in argument list. By default, `marker` is set to 0, which turns the support off.

```c++
assert(!parser.uses_answer_file());
parser.use_answer_file();
assert(parser.uses_answer_file());
```
When turned on, will use the marker character to recognize the name of the answer file and use further arguments from there. For instance, if a program decides to support answer files and file named `options` contains this text:
```
-vv
-v
--arg1=value1
--arg2
value2
--arg3=value3
--arg4
```
Then those calls are equivalent:
```
$ ./prog -vvv --arg1 value1 --arg2 value2 --arg3=value3 --arg4
$ ./prog @options
```
### parser::provide_help
```cxx
void provide_help(bool value = true);
bool provide_help() const noexcept;
void provide_help(bool value);
bool provides_help() const noexcept;
```

Turns off/on the built in support for `"-h"` and `"--help"` arguments.
Expand Down
27 changes: 19 additions & 8 deletions include/args/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ namespace args {
std::string prog_;
std::string usage_;
bool provide_help_ = true;
char answer_file_marker_{};
std::optional<size_t> parse_width_ = {};
base_translator const* tr_;
[[nodiscard]] std::string _(lng id,
Expand All @@ -96,14 +97,16 @@ namespace args {

[[nodiscard]] std::pair<size_t, size_t> count_args() const noexcept;

bool parse_long(std::string_view const& name,
unsigned& i,
unknown_action on_unknown);
bool parse_short(std::string_view const& name,
unsigned& i,
unknown_action on_unknown);
bool parse_positional(std::string_view const& value,
template <typename ArgList>
bool parse_list(ArgList& list, unknown_action on_unknown);
template <typename ArgList>
bool parse_long(ArgList& list, unknown_action on_unknown);
template <typename ArgList>
bool parse_short(ArgList& list, unknown_action on_unknown);
bool parse_positional(std::string const& value,
unknown_action on_unknown);
bool parse_answer_file(std::string const& value,
unknown_action on_unknown);

template <typename Action, typename... Args>
actions::builder add(Args&&... args) {
Expand Down Expand Up @@ -190,7 +193,15 @@ namespace args {
std::string const& usage() const noexcept;

void provide_help(bool value = true) { provide_help_ = value; }
bool provide_help() const noexcept { return provide_help_; }
bool provides_help() const noexcept { return provide_help_; }

void use_answer_file(char marker = '@') {
answer_file_marker_ = marker;
}
bool uses_answer_file() const noexcept {
return answer_file_marker_ != 0;
}
char answer_file_marker() const noexcept { return answer_file_marker_; }

arglist const& args() const noexcept { return args_; }

Expand Down
3 changes: 2 additions & 1 deletion include/args/translator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace args {
needed_enum_unknown,
needed_enum_known_values,
required,
error_msg
error_msg,
file_not_found
};

struct base_translator {
Expand Down
Loading

0 comments on commit 7d74bee

Please sign in to comment.