-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Enum support for the argument parser.
- Loading branch information
1 parent
78389d5
commit 840e08c
Showing
9 changed files
with
458 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
test/snippet/argument_parser/custom_argument_parsing_enumeration.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <system_error> | ||
|
||
#include <seqan3/argument_parser/all.hpp> | ||
|
||
namespace seqan3::custom | ||
{ | ||
// Specialise the seqan3::custom::argument_parsing data structure to enable parsing of std::errc. | ||
template <> | ||
struct argument_parsing<std::errc> | ||
{ | ||
// Specialise a mapping from an identifying string to the respective value of your type Foo. | ||
static inline const std::unordered_map<std::string_view, std::errc> enumeration_names | ||
{{ | ||
{"no_error", std::errc{}}, | ||
{"timed_out", std::errc::timed_out}, | ||
{"invalid_argument", std::errc::invalid_argument}, | ||
{"io_error", std::errc::io_error} | ||
}}; | ||
}; | ||
|
||
} // namespace seqan3::custom | ||
|
||
int main(int argc, char const * argv[]) | ||
{ | ||
std::errc value{}; | ||
|
||
seqan3::argument_parser parser{"my_program", argc, argv}; | ||
|
||
// Because of the argument_parsing struct and | ||
// the static member function enumeration_names | ||
// you can now add an option that takes a value of type std::errc: | ||
parser.add_option(value, 'e', "errc", "Give me a std::errc value."); | ||
|
||
try | ||
{ | ||
parser.parse(); | ||
} | ||
catch (seqan3::parser_invalid_argument const & ext) // the user did something wrong | ||
{ | ||
std::cerr << "[PARSER ERROR] " << ext.what() << "\n"; // customize your error message | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.