Skip to content

Commit

Permalink
Merge branch 'positional_arguments_with_Fire' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
kongaskristjan committed Oct 2, 2020
2 parents d529360 + b7b9636 commit 65fffaf
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 209 deletions.
16 changes: 5 additions & 11 deletions README.md
Expand Up @@ -97,13 +97,10 @@ This is what you perceive as the program entry point. All arguments must be `boo

### <a id="fire"></a> D.1 FIRE(...) and FIRE_NO_SPACE_ASSIGNMENT(...)

`FIRE(...)` and `FIRE_NO_SPACE_ASSIGNMENT(...)` both create the main function to parse arguments and call `...`, however, they differ in how arguments are parsed. `FIRE(...)` parses `program -x 1` as `program -x=1`, but `FIRE_NO_SPACE_ASSIGNMENT(...)` parses `-x` as a flag and `1` as a positional argument.
`FIRE(...)` creates the main function that parses arguments and calls `...`.
`FIRE_NO_SPACE_ASSIGNMENT(...)` disallows space assignments, eg. `-x 1` must be written as `-x=1`.
`FIRE_NO_SPACE_ASSIGNMENT(...)` avoids using exceptions, so if your program has them disabled, use this.

To use positional or vector arguments, `FIRE_NO_SPACE_ASSIGNMENT(...)` must be used. There are two reasons:

* Mixing positional and named arguments with space-separated values makes a bad CLI anyway, eg: `program a -x b c` doesn't seem like `-x=b` with `a` and `c` as positional.
* Implementing such a CLI within Fire API is likely impossible without using exceptions.

### D.2 <a id="fire_arg"></a> fire::arg(identifiers[, default_value]])

#### <a id="identifier"></a> D.2.1 Identifiers
Expand Down Expand Up @@ -230,18 +227,15 @@ v0.1 release is tested on:

#### Current status

* Support positional arguments in FIRE(...):
* Exception based introspection of fired_main arguments
* Allow positional arguments in FIRE() if introspection revealed that fire::arg("-x") is converted to non-bool
* Ensure FIRE_NO_SPACE_ASSIGNMENT() still compiles without exceptions
* Solve Windows non-ascii character input
* Automatic testing for error messages
* Improve help messages
* Refactor `log_elem::type` from `std::string` -> `enum class`
* Help messages: separate positional arguments, named arguments and flags in `Usage`
* Program description
* Ensure API user gets an error message when using required positional arguments after optional positional arguments

#### v0.2 release

* Subcommands (eg. `git add` and `git show`, which may have different flags/options)
* Self-defined objects with string streams
* `save(...)` keyword enclosing `arg`, which will save the program from exiting even if not all required arguments are present or correct (eg. for `--version`)
7 changes: 7 additions & 0 deletions examples/CMakeLists.txt
Expand Up @@ -7,4 +7,11 @@ add_executable(optional_and_default optional_and_default.cpp ../fire.hpp)
add_executable(positional positional.cpp ../fire.hpp)
add_executable(vector_positional vector_positional.cpp ../fire.hpp)

if(MSVC)
# FIXIT: Test exception handling on Windows too
else()
add_executable(no_exceptions no_exceptions.cpp ../fire.hpp)
target_compile_options(no_exceptions PRIVATE -fno-exceptions)
endif()

set(EXAMPLES_BUILD_DIR $<TARGET_FILE_DIR:basic> PARENT_SCOPE)
2 changes: 1 addition & 1 deletion examples/all_combinations.cpp
Expand Up @@ -29,4 +29,4 @@ int fired_main(
return 0;
}

FIRE_NO_SPACE_ASSIGNMENT(fired_main)
FIRE(fired_main)
35 changes: 35 additions & 0 deletions examples/no_exceptions.cpp
@@ -0,0 +1,35 @@

/*
Copyright (c) 2020 Kristjan Kongas
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/

#include <iostream>
#include "../fire.hpp"

using namespace std;

int fired_main(
unsigned repeats = fire::arg({"-r", "--repeats", "Number of repeats"}, 1),
vector<string> strings = fire::arg::vector("Optional positive integer")
) {
for(unsigned i = 0; i < repeats; ++i) {
for(string &s: strings) {
cout << s << " ";
}
cout << endl;
}
return 0;
}

FIRE_NO_SPACE_ASSIGNMENT(fired_main)
2 changes: 1 addition & 1 deletion examples/positional.cpp
Expand Up @@ -27,4 +27,4 @@ int fired_main(
return 0;
}

FIRE_NO_SPACE_ASSIGNMENT(fired_main)
FIRE(fired_main)
2 changes: 1 addition & 1 deletion examples/vector_positional.cpp
Expand Up @@ -41,4 +41,4 @@ int fired_main(vector<string> strings = fire::arg::vector("strings to be printed
return 0;
}

FIRE_NO_SPACE_ASSIGNMENT(fired_main)
FIRE(fired_main)

0 comments on commit 65fffaf

Please sign in to comment.