toad.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
Mastodon server operated by David Troy, a tech pioneer and investigative journalist addressing threats to democracy. Thoughtful participation and discussion welcome.

Administered by:

Server stats:

230
active users

#argparse

0 posts0 participants0 posts today

It's probably a case of #bikesheding, but I've been really unhappy with the #python 's #argparse library for a while now. It's okay for run of the mill tools, but fails for more complex command line tools that have subcommands and complex options like --no-except to negate an option.

This is my first cut writing a command line parser for a #cli

github.com/sumanthvepa/experim

GitHubexperiments/python/parse-dralithus-cmdline at develop · sumanthvepa/experimentsExperiments is a repository of programming experiments that I written to explore various aspects of programming and technology - sumanthvepa/experiments

TIL: #Python's #argparse module cannot parse arguments that start with a dash. It always expects them to be a flag and then throws an error if that flag is not defined.

github.com/python/cpython/issu

My use case: I have a command with subparsers/subcommands, and I'd like one of these subcommands to just take all of argv that follows its name and pass it on to someplace else.

mycmd subcmd foo -bar --baz

subcmd.add_argument("args", nargs="*") doesn't work.

Guess I'll have to resort to a hack.

GitHubargparse.REMAINDER doesn't work as first argument · Issue #61252 · python/cpythonBy cjerdonek

If you’re using #Python’s #argparse module to parse CLI arguments and want to have `--foo` and `--no-foo` flags, check out `action=argparse.BooleanOptionalAction`, which automatically creates `--no-foo` for you: docs.python.org/3/library/argp

parser.add_argument('--foo', action=argparse.BooleanOptionalAction)

Requires 3.9 though. Simple solution for 3.8:

parser.add_argument('--foo', action='store_true')
parser.add_argument('--no-foo', dest='foo', action='store_false')
parser.set_defaults(foo=True)

Python documentationargparse — Parser for command-line options, arguments and subcommandsSource code: Lib/argparse.py Tutorial: This page contains the API reference information. For a more gentle introduction to Python command-line parsing, have a look at the argparse tutorial. The arg...