Answers for "python argparse options list"

2

argeparse can it take a type list

import argparse

parser = argparse.ArgumentParser()

# By default it will fail with multiple arguments.
parser.add_argument('--default')

# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)

# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')

# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')

# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)

# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')

# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)
Posted by: Guest on November-04-2020
1

argparse list of options

parser.add_argument('--list',
                    default='all',
                    const='all',
                    nargs='?',
                    choices=['servers', 'storage', 'all'],
                    help='list servers, storage, or both (default: %(default)s)')
Posted by: Guest on November-09-2020
0

argparse list

parser.add_argument('-l','--list', nargs='+', help='<Required> Set flag', 
                    required=True)
# Use like:
# python arg.py -l 1234 2345 3456 4567

parser.add_argument('-l','--list', action='append', help='<Required> Set flag', 
                    required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567

'''
Notes: 
1. nargs='+' takes 1 or more arguments, nargs='*' takes zero or more.
2. Don't use type=list!!! - There is probably no situation where you would want
   to use type=list with argparse. Ever.
'''
Posted by: Guest on October-19-2021
3

argparse python

# Generic parser function intialization in PYTHON
def create_parser(arguments):
    """Returns an instance of argparse.ArgumentParser"""
    # your code here
    
    parser = argparse.ArgumentParser(
        description="Description of your code")
    parser.add_argument("argument", help="mandatory or positional argument")
    parser.add_argument("-o", "--optional", 
    	help="Will take an optional argument after the flag")
    namespace = parser.parse_args(arguments)
    
    # Returns a namespace object with your arguments
    return namespace
Posted by: Guest on December-09-2020
0

parser.add_argument array python

parser.add_argument('-l','--list', nargs='+', help='<Required> Set flag', required=True)
# Use like:
# python arg.py -l 1234 2345 3456 4567
Posted by: Guest on April-13-2020

Python Answers by Framework

Browse Popular Code Answers by Language