-
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] Multi-level argument parsing.
- Loading branch information
1 parent
b10205d
commit e22099c
Showing
12 changed files
with
336 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# How to write a multi-level argument parser {#multi_level_arg_parse} | ||
|
||
[TOC] | ||
|
||
This HowTo shows you how to write a multi-level argument parser using SeqAn3. | ||
|
||
\tutorial_head{Easy, 15 min, \ref tutorial_argument_parser, } | ||
|
||
# Motivation | ||
|
||
A common use case for command line tools, e.g. `git`, is to have multiple subcommands, e.g. `git fetch` or `git push`. | ||
Each subcommand has its own set of options and its own help page. | ||
This HowTo explains how this can be done with the seqan3::argument_parser and serves as a copy'n'paste source. | ||
If you are new to SeqAn, we recommend to do the basic | ||
\link tutorial_argument_parser argument parser tutorial \endlink before you read further. | ||
|
||
# A multi-level argument parser | ||
|
||
In order to keep multi-level parsing straightforward and simple, | ||
the seqan3::argument_parser provides an advanced interface that internally takes care of the correct input parsing. | ||
|
||
You simply need to specify the names of the subcommands when constructing your top-level argument parser: | ||
|
||
\snippet doc/howto/multi_level_argument_parser/multi_level_arg_parse.cpp construction | ||
|
||
You can still add options and flags to your top-level parser if needed. | ||
After calling seqan3::argument_parser::parse() on your top-level parser, | ||
you can then access the sub-parser via the function seqan3::argument_parser::get_sub_parser(): | ||
|
||
\snippet doc/howto/multi_level_argument_parser/multi_level_arg_parse.cpp get_sub_parser | ||
|
||
The sub-parser's **seqan3::argument_parser::info::app_name will be set to the user chosen sub command**. | ||
For example, if the user calls | ||
|
||
``` | ||
max$ ./mygit push -h | ||
``` | ||
|
||
then the sub-parser will be named `mygit-push` and will be instantiated with all arguments | ||
followed by the keyword `push` which in this case triggers printing the help page (`-h`). | ||
|
||
That's it. Here is a full example of a multi-level argument parser you can try and adjust to your needs: | ||
|
||
\include doc/howto/multi_level_argument_parser/multi_level_arg_parse.cpp |
97 changes: 97 additions & 0 deletions
97
doc/howto/multi_level_argument_parser/multi_level_arg_parse.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,97 @@ | ||
#include <seqan3/argument_parser/all.hpp> | ||
|
||
using namespace seqan3; | ||
|
||
// ===================================================================================================================== | ||
// pull | ||
// ===================================================================================================================== | ||
|
||
struct pull_arguments | ||
{ | ||
std::string repository{}; | ||
std::string branch{}; | ||
bool progress{false}; | ||
}; | ||
|
||
int run_git_pull(argument_parser & parser) | ||
{ | ||
pull_arguments args{}; | ||
|
||
parser.add_positional_option(args.repository, "The repository name to pull from."); | ||
parser.add_positional_option(args.branch, "The branch name to pull from."); | ||
|
||
try | ||
{ | ||
parser.parse(); | ||
} | ||
catch (parser_invalid_argument const & ext) | ||
{ | ||
debug_stream << "[Error] " << ext.what() << "\n"; | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
// ===================================================================================================================== | ||
// push | ||
// ===================================================================================================================== | ||
|
||
struct push_arguments | ||
{ | ||
std::string repository{}; | ||
std::vector<std::string> branches{}; | ||
bool push_all{false}; | ||
}; | ||
|
||
int run_git_push(argument_parser & parser) | ||
{ | ||
push_arguments args{}; | ||
|
||
parser.add_positional_option(args.repository, "The repository name to push to."); | ||
parser.add_positional_option(args.branches, "The branch names to push (if none are given, push current)."); | ||
|
||
try | ||
{ | ||
parser.parse(); | ||
} | ||
catch (parser_invalid_argument const & ext) | ||
{ | ||
debug_stream << "[Error] " << ext.what() << "\n"; | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int main(int argc, char const ** argv) | ||
{ | ||
//![construction] | ||
argument_parser top_level_parser{"mygit", argc, argv, true, {"push", "pull"}}; | ||
//![construction] | ||
|
||
// Add information and options to your top-level parser just as you would do with a normal one. | ||
// Note that all options directed at the top-level parser must be specified BEFORE the subcommand key word. | ||
top_level_parser.info.description.push_back("You can push or pull from a remote repository."); | ||
bool flag{false}; | ||
top_level_parser.add_option(flag, 'f', "flag", "some flag"); | ||
|
||
try | ||
{ | ||
top_level_parser.parse(); // trigger command line parsing | ||
} | ||
catch (parser_invalid_argument const & ext) // catch user errors | ||
{ | ||
debug_stream << "[Error] " << ext.what() << "\n"; // customise your error message | ||
return -1; | ||
} | ||
|
||
//![get_sub_parser] | ||
argument_parser & sub_parser = top_level_parser.get_sub_parser(); // hold a reference to the sub_parser | ||
//![get_sub_parser] | ||
|
||
if (sub_parser.info.app_name == std::string_view{"git-pull"}) | ||
run_git_pull(sub_parser); | ||
else if (sub_parser.info.app_name == std::string_view{"git-push"}) | ||
run_git_push(sub_parser); | ||
} |
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
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
Oops, something went wrong.