Linked Questions
12 questions linked to/from Argparse with required subparser
0
votes
1
answer
465
views
how can I make subparsers be required? [duplicate]
I would like to make sure at least one of the sub commands is selected. But there is no required option for add_subparsers() how can I enforce at least one subparser is selected?
Currently I did this ...
Wang's user avatar
- 8,446
40
votes
4
answers
20k
views
argparse with required subcommands
With python's argparse, how do I make a subcommand a required argument? I want to do this because I want argparse to error out if a subcommand is not specified. I override the error method to print ...
18
votes
6
answers
18k
views
Python argparse: args has no attribute func
Intro
I'm trouble for a school project. I'm making a testsuit and i'm needing bot a configuration generation interface and a test runner. For that i used the library argparse and two subparsers cgi ...
17
votes
1
answer
6k
views
Why does this argparse code behave differently between Python 2 and 3?
The following code, using argparse's subparsers, fails on Python 3 but runs as expected in Python 2. After comparing the docs, I still can't tell why.
#!/usr/bin/env python
from __future__ import ...
2
votes
2
answers
8k
views
How to Set a Default Subparser using Argparse Module with Python 2.7
I'm using Python 2.7 and I'm trying to accomplish a shell like behavior using argparse.
My issue, in general, that I cannot seem to find a way, in Python 2.7, to use argparse's subparsers as optional.
...
0
votes
1
answer
5k
views
__init__() got an unexpected keyword argument 'required' using argparse in Python 2 [duplicate]
I have below python script which if I run with Python 2.7.16 version then I get an error but if I run with Python 3 version then it works fine.
Below is my code:
from argparse import ArgumentParser
...
0
votes
1
answer
2k
views
Optional subparsers?
I have a utility which allows a user to read their ~/.aws/credentials file and export environment variables.
Currently, the CLI interface looks like this:
usage: aws-env [-h] [-n] profile
Extract ...
1
vote
1
answer
2k
views
Create parser with subcommands in argparse, customize positional argument(s)
I'm very new to this module so please bear with me. I have the following code:
reader.py
import argparse
parent_parser = argparse.ArgumentParser(description="Read text files.")
parent_parser....
0
votes
1
answer
901
views
argparse with optional subcommands not working as expected
Using python version 3.9.5 I have a problem with the following code snippet:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(required=False)
sub_parser = ...
2
votes
1
answer
2k
views
Python Argparse, How to Properly Organize ArgParse Code
I haven't used argparse in 10 years, but I understand it and what I have below does work as I want it to work... but this is going to get a lot more complicated as I continue adding commands, parsers ...
2
votes
1
answer
142
views
How to stack up multiple subcommands and have an implied default command using argparse?
In my example below, I am trying to setup the following command line scenarios:
$ myapp.py flowers [-h]
$ myapp.py flowers plants [-h]
$ myapp.py flowers plants add [-h]
The actual outcomes are:
$ ...
0
votes
0
answers
37
views
Add some argument if and only if the parser has not any subparsers
The commandline interface that I want is something like this:
my-executable command REQUIRED_ARG
# or
my-executable command subcommand [--foo] [--bar]
So, I don't want REQUIRED_ARG to be required (...