🌐
Python
docs.python.org › 3 › library › argparse.html
argparse — Parser for command-line options, arguments and subcommands
If no long option strings were supplied, dest will be derived from the first short option string by stripping the initial - character. Any internal - characters will be converted to _ characters to make sure the string is a valid attribute name. The examples below illustrate this behavior: >>> parser = argparse.ArgumentParser() >>> parser.add_argument('-f', '--foo-bar', '--foo') >>> parser.add_argument('-x', '-y') >>> parser.parse_args('-f 1 -x 2'.split()) Namespace(foo_bar='1', x='2') >>> parser.parse_args('--foo 1 -y 2'.split()) Namespace(foo_bar='1', x='2')
🌐
Educative
educative.io › answers › what-is-argparse-dest-in-python
What is argparse dest in Python?
It displays the generic usage of the program, help, and errors. The parse_args() function of the ArgumentParser class parses arguments and adds value as an attribute dest of the object.
🌐
Nokia
network.developer.nokia.com › static › sr › learn › pysros › latest › argparse.html
argparse – Argument parser functions — pySROS 26.3.1 documentation
If no long option strings were supplied, dest will be derived from the first short option string by stripping the initial - character. Any internal - characters will be converted to _ characters to make sure the string is a valid attribute name. The examples below illustrate this behavior: >>> parser = argparse.ArgumentParser() >>> parser.add_argument('-f', '--foo-bar', '--foo') >>> parser.add_argument('-x', '-y') >>> parser.parse_args('-f 1 -x 2'.split()) Namespace(foo_bar='1', x='2') >>> parser.parse_args('--foo 1 -y 2'.split()) Namespace(foo_bar='1', x='2')
🌐
Stack Overflow
stackoverflow.com › questions › 71851214 › argparse-add-argument-with-choices-and-save-to-variable-dest
python - Argparse add argument with choices and save to variable (dest) - Stack Overflow
import argparse parser = argparse.ArgumentParser() parser.add_parser('title', help='Top level arg') subparser = parser.add_subparsers(dest='my_var') # so I can access it later subcommand_parser = subparser.add_parser('subtitle', help='Another option help', choices=['foo', 'bar'])
🌐
Python Module of the Week
pymotw.com › 2 › argparse
argparse – Command line option and argument parsing. - Python Module of the Week
The object holds the argument values as attributes, so if your argument dest is "myoption", you access the value as args.myoption. Here is a simple example with 3 different options: a boolean option (-a), a simple string option (-b), and an integer option (-c). import argparse parser = argparse.ArgumentParser(description='Short sample app') parser.add_argument('-a', action="store_true", default=False) parser.add_argument('-b', action="store", dest="b") parser.add_argument('-c', action="store", dest="c", type=int) print parser.parse_args(['-a', '-bval', '-c', '3'])
🌐
Python Module of the Week
pymotw.com › 3 › argparse
argparse — Command-Line Option and Argument Parsing
December 30, 2016 - The object holds the argument values as attributes, so if the argument’s dest is set to "myoption", the value is accessible as args.myoption. Here is a simple example with three different options: a Boolean option (-a), a simple string option (-b), and an integer option (-c). ... import argparse parser = argparse.ArgumentParser(description='Short sample app') parser.add_argument('-a', action="store_true", default=False) parser.add_argument('-b', action="store", dest="b") parser.add_argument('-c', action="store", dest="c", type=int) print(parser.parse_args(['-a', '-bval', '-c', '3']))
🌐
GitHub
github.com › python › cpython › issues › 103639
argparse: Add the keyword argument `subspace_dest` of type `Optional[str]` to `argparse._SubParsersAction.add_parser` to allow sub-namespaces which correspond to subparsers · Issue #103639 · python/cpython
April 20, 2023 - Any dest parameters specified in each of the subprogram's parsers can be done without fear of colliding with sibling parsers. After parsing the CLI arguments, the sub-Namespace can be passed to the corresponding subprogram, as will be demonstrated below. #!/usr/bin/env python3 import argparse def get_cli_args(argv) -> argparse.Namespace: parser = argparse.ArgumentParser(prog="doorctl", description = "open and close doors") parser_action = parser.add_subparsers(title="action", dest="action", required=True) parser_action_open = parser_action.add_parser("open") parser_action_open.add_argument("ac
Author   jb2170
🌐
Read the Docs
stackless.readthedocs.io › en › 2.7-slp › library › argparse.html
15.4. argparse — Parser for command-line options, arguments and sub-commands — Stackless-Python 2.7.15 documentation
If no long option strings were supplied, dest will be derived from the first short option string by stripping the initial - character. Any internal - characters will be converted to _ characters to make sure the string is a valid attribute name. The examples below illustrate this behavior: >>> parser = argparse.ArgumentParser() >>> parser.add_argument('-f', '--foo-bar', '--foo') >>> parser.add_argument('-x', '-y') >>> parser.parse_args('-f 1 -x 2'.split()) Namespace(foo_bar='1', x='2') >>> parser.parse_args('--foo 1 -y 2'.split()) Namespace(foo_bar='1', x='2')
🌐
ZetCode
zetcode.com › python › argparse
Python argparse - parsing command line arguments in Python with argparse module
September 24, 2024 - In this article we show how to parse command line arguments in Python with argparse module.
🌐
Medium
medium.com › geekculture › argparse-python-command-line-parsing-tool-370e6d97cee3
argparse — Python Command Line Parsing Tool | by Tony | Geek Culture | Medium
January 30, 2023 - import argparse # 1. Create parser parse = argparse.ArgumentParser( description="This is my parser" ) # 2. Add argument(s) to parser parse.add_argument("-n", dest="name") parse.add_argument("-a", dest="age") # 3. Parsing arguments args = parse.parse_args() # Print values print(args.name) print(args.age) And let’s execute the above code: $ python3 argparse_sample.py -n Tony -a 100 Tony 100 ·
Find elsewhere
Top answer
1 of 2
1

Though you don't typically need it, add_argument returns an instance of the Action subclass used to implement the argument. Among other things, these objects have a dest attribute containing the string you want.

For example,

import argparse

parser = argparse.ArgumentParser()
a1 = parser.add_argument('arg1')
a2 = parser.add_argument('arg2')
a3 = parser.add_argument('arg3')
a4 = parser.add_argument('arg4')
     
destinations = [x.dest for x in [a1, a2, a3, a4]])

parser._actions contains a list of all arguments defined for the parser, including things like --help, so that may also be of use.

2 of 2
1
In [1]: import argparse    
In [2]: parser = argparse.ArgumentParser()
   ...: parser.add_argument('arg1')
   ...: parser.add_argument('arg2')
   ...: parser.add_argument('arg3')
   ...: parser.add_argument('arg4')

Since these are all required positionals, we can call parse_args with:

In [4]: parser.parse_args('1 2 3 4'.split())
Out[4]: Namespace(arg1='1', arg2='2', arg3='3', arg4='4')

The parser maintains a list of the actions (arguments) that you defined. Their dest can be listed with:

In [5]: [a.dest for a in parser._actions]
Out[5]: ['help', 'arg1', 'arg2', 'arg3', 'arg4']

That list includes the 'help' that was added by default.

Another list, derived from the args (converted to a dict)

In [6]: list(vars(parser.parse_args('1 2 3 4'.split())).keys())
Out[6]: ['arg1', 'arg2', 'arg3', 'arg4']

You could also collect your own list of actions (some companies don't like you to access the "private" attributes like '_actions', though Python has a loose concept of public v private variables).

In [9]: parser = argparse.ArgumentParser()
   ...: a1=parser.add_argument('arg1')
   ...: a2=parser.add_argument('arg2')
   ...: a3=parser.add_argument('arg3')
   ...: a4=parser.add_argument('arg4')

In [10]: [a.dest for a in [a1,a2,a3,a4]]
Out[10]: ['arg1', 'arg2', 'arg3', 'arg4']
🌐
Python for Network Engineers
pyneng.readthedocs.io › en › latest › book › additional_info › argparse.html
argparse - Python for network engineers
In addition to nested parsers, there are also several new features of argparse in this example. Parser create_parser uses a new argument - metavar: create_parser.add_argument('-n', metavar='db-filename', dest='name', default=DFLT_DB_NAME, help='db filename') create_parser.add_argument('-s', dest='schema', default=DFLT_DB_SCHEMA, help='db schema filename') Argument metavar allows you to specify argument name to show it in usage message and help: $ python parse_dhcp_snooping.py create_db -h usage: parse_dhcp_snooping.py create_db [-h] [-n db-filename] [-s SCHEMA] optional arguments: -h, --help show this help message and exit -n db-filename db filename -s SCHEMA db schema filename ·
🌐
Python documentation
docs.python.org › 3 › howto › argparse.html
Argparse Tutorial — Python 3.14.4 documentation
author, Tshepang Mbambo,. This tutorial is intended to be a gentle introduction to argparse, the recommended command-line parsing module in the Python standard library. Concepts: Let’s show the sor...
🌐
Real Python
realpython.com › command-line-interfaces-python-argparse
Build Command-Line Interfaces With Python's argparse – Real Python
December 14, 2024 - When building Python command-line interfaces (CLI), Python’s argparse module offers a comprehensive solution. You can use argparse to create user-friendly command-line interfaces that parse arguments and options directly from the command line.
🌐
Medium
kegui.medium.com › argparse-tutorial-4c576de37268
Argparse Tutorial. this is the best argparse tutorial I… | by Ke Gui | Medium
June 6, 2020 - dest — The name of the attribute to be added to the object returned by parse_args(). Argparse Tutorial · AI · 365 followers · ·141 following · An ordinary guy who wants to be the reason someone believes in the goodness of people.
🌐
GitHub
github.com › python › cpython › issues › 101990
argparse: positional with `nargs=*` results in ignored option argument if they share a `dest` · Issue #101990 · python/cpython
February 17, 2023 - import argparse parser = argparse.ArgumentParser() parser.add_argument('foo', nargs='*') parser.add_argument('--foo') print(vars(parser.parse_args())) Passing positional arguments works as expected. ❯ python /tmp/test.py bar {'foo': ['bar']} However, the option arguments are ignored. ❯ python /tmp/test.py --foo bar {'foo': []} As a workaround, you can set a separate dest and handle this in your application.
Author   stephenfin
🌐
CRAN
cran.r-project.org › web › packages › argparse › vignettes › argparse.html
argparse Command Line Argument Parsing
If not, see <http://www.gnu.org/licenses/>. suppressPackageStartupMessages(library("argparse")) # create parser object parser <- ArgumentParser() # specify our desired options # by default ArgumentParser will add an help option parser$add_argument( "-v", "--verbose", action = "store_true", default = TRUE, help = "Print extra output [default]" ) parser$add_argument( "-q", "--quietly", action = "store_false", dest = "verbose", help = "Print little output" ) parser$add_argument( "-c", "--count", type = "integer", default = 5, help = "Number of random normals to generate [default %(default)s]", me