Skip to content

Commit

Permalink
Add names to positional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
kongaskristjan committed Aug 6, 2020
1 parent 46d7e3e commit 65864b7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -122,6 +122,11 @@ Identifiers are used to find arguments from command line and provide a descripti
* Example: `int fired_main(int x = fire::arg(0));`
* CLI usage: `program 1`


* Example: `int fired_main(int x = fire::arg(0, "<first>"));`
* CLI usage: `program 1`
* name `<first>` appears in help and error messages

#### <a id="description"></a> D.2.2 Descrpition (in identifier)

Argument description for `--help` message. Is determined by not having leading hyphens.
Expand Down Expand Up @@ -202,7 +207,6 @@ v0.1 release is tested on:

#### Current status

* Names for positional arguments for help message
* Automatic testing for error messages
* Improve help messages
* Refactor `log_elem::type` from `std::string` -> `enum class`
Expand Down
4 changes: 2 additions & 2 deletions examples/positional.cpp
Expand Up @@ -20,8 +20,8 @@
using namespace std;

int fired_main(
int x0 = fire::arg({0, "Zeroth argument"}),
fire::optional<int> x1 = fire::arg({1, "First argument"})
int x0 = fire::arg({0, "<first>", "First argument"}),
fire::optional<int> x1 = fire::arg({1, "<second>", "Second argument"})
) {
std::cout << x0 << " " << x1.value_or(0) << endl;
return 0;
Expand Down
16 changes: 14 additions & 2 deletions fire.hpp
Expand Up @@ -72,7 +72,7 @@ namespace fire {

class identifier {
optional<int> _pos;
optional<std::string> _short_name, _long_name, _descr;
optional<std::string> _short_name, _long_name, _pos_name, _descr;
bool _vector = false;
bool _optional = false; // Only use for operator<

Expand Down Expand Up @@ -305,6 +305,11 @@ namespace fire {
inline identifier::identifier(const std::vector<std::string> &names, optional<int> pos) {
// Find description, shorthand and long name
for(const std::string &name: names) {
if(name.size() >= 2 && name.front() == '<' && name.back() == '>') {
_pos_name = name;
continue;
}

int hyphens = count_hyphens(name);
_instant_assert(hyphens <= 2, "Identifier entry " + name + " must prefix either:"
" 0 hyphens for description,"
Expand Down Expand Up @@ -347,10 +352,17 @@ namespace fire {
_instant_assert(! _long_name.has_value(),
"Can't specify both name " + _long_name.value_or("") + " and index " + std::to_string(pos.value()));
_pos = pos;
_longer = _help = "<" + std::to_string(pos.value()) + ">";
if(_pos_name.has_value())
_longer = _help = _pos_name.value();
else
_longer = _help = "<" + std::to_string(pos.value()) + ">";
}
_instant_assert(_short_name.has_value() || _long_name.has_value() || _pos.has_value(),
"Argument must be specified with at least on of the following: shorthand, long name or index");

if(_pos_name.has_value())
_instant_assert(_pos.has_value(),
"Positional name " + _pos_name.value_or("") + " requires the argument to be positional");
}

bool identifier::operator<(const identifier &other) const {
Expand Down
5 changes: 4 additions & 1 deletion tests/tests.cpp
Expand Up @@ -185,7 +185,9 @@ TEST(identifier, help) {
EXPECT_EQ(identifier(vector<string>{"-l", "--long"}, empty).help(), "-l|--long");
EXPECT_EQ(identifier(vector<string>{"--long"}, empty).help(), "--long");

EXPECT_EQ(identifier(vector<string>{"zeroth"}, 0).help(), "<0>");
EXPECT_EQ(identifier(vector<string>{"description"}, 0).help(), "<0>");
EXPECT_EQ(identifier(vector<string>{"<name>", "description"}, 0).help(), "<name>");
EXPECT_EQ(identifier(vector<string>{"<name>"}, 0).help(), "<name>");
EXPECT_EQ(identifier().help(), "...");
}

Expand All @@ -197,6 +199,7 @@ TEST(identifier, longer) {
EXPECT_EQ(identifier(vector<string>{"--long"}, empty).longer(), "--long");

EXPECT_EQ(identifier(vector<string>{"zeroth"}, 0).longer(), "<0>");
EXPECT_EQ(identifier(vector<string>{"<name>"}, 0).longer(), "<name>");
EXPECT_EQ(identifier().longer(), "...");
}

Expand Down

0 comments on commit 65864b7

Please sign in to comment.