Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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)
Comment

PREVIOUS NEXT
Code Example
Python :: python try catch 
Python :: np.zeros data type not understood 
Python :: how to create empty series in pandas 
Python :: Simple way to measure cell execution time in ipython notebook 
Python :: dataframe get index name 
Python :: python ssh connection 
Python :: for loop in django 
Python :: how to take input from user in python 
Python :: isnumeric python 
Python :: stock market api python 
Python :: how to make a class in python 
Python :: how to use timeit in python 3 
Python :: own labels for ticks matplotlib 
Python :: pandas most frequent value 
Python :: mac why is python installed in usr and application 
Python :: get pixel color pygame 
Python :: webbrowser python 
Python :: python find all elements of substring in string 
Python :: round down a number python 
Python :: playsound error python 
Python :: convert pandas dataframe/ table to python dictionary 
Python :: how to change turtle shape in python 
Python :: nohup python command for linux 
Python :: pylint import error 
Python :: generate new secret key django 
Python :: python get pixel color from screen 
Python :: python moving average pandas 
Python :: anagram program in python 
Python :: python adding digits 
Python :: pd df drop columns 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =