by akvadrako · 6y ago · 0 comments
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
a = subparsers.add_parser('a')
a.add_argument('bar')
A better API would look more like this: Parser(Subparsers({
'a': Parser(Arg('bar')),
}))
On top of that it has a number of bugs in edge cases they refuse to document or fix. For example, you can't have a subcommand that always collects all the remaining arguments as-is. That's a pretty basic use-case when you want to pass those arguments to another program.But I reject the idea that imperative is bad. You can compose imperative programming fine.
def my_parser(parser):
parser.add_argument('bar')
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
my_parser(subparsers.add_parser('a'))
my_parser(subparsers.add_parser('a2'))
> you can't have a subcommand that always collects all the remaining arguments as-is. parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
a = subparsers.add_parser('a')
a.add_argument('args', nargs='*')
That works just fine. ./example a arg1 arg2 arg3
./example a -- arg1 --look-ma-hyphens-arg2 arg3
If you're complaining about the double hyphen syntax....that's a universal convention for UNIX-ish CLI positional arguments. Otherwise things get really murky. ./example a --help
Did I want help on the `a` subcommand, or did I want to pass `--help` to the `a` subcommand? The `--` syntax lets you distinguish.JavaScript has functional origins, and in practice quite a bit of JavaScript is written in a functional-lite style. As a JS developer, I find it quite infuriating that Python doesn't support these patterns, as they make code a lot more readable, and it seems to be a matter of principle rather than a technical limitation.
It is very powerful, uses special files stdin/stdout when file argument is "-", supports groups, subcommands...