Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python argparse lists flags as optional even with required

# Short answer:
# With argparse, parameters starting with - or -- are considered optional by
# default.

# Longer answer:
# With argparse, parameters starting with - or -- are considered optional by
# default. All other parameters are positional parameters and are required
# by default. It is possible to require optional arguments, but this is a bit
# against their design. Since they are still part of the non-positional
# arguments, they will still be listed under the confusing header 
# “optional arguments” even if they are required. The missing square brackets
# in the usage part however show that they are indeed required.

# Solution:
# I create three categories as follows:
parser._action_groups.pop() # remove existing groups
required_pos = parser.add_argument_group('Required Positional Arguments')
required_nam = parser.add_argument_group('Required Named Arguments')
optional = parser.add_argument_group('Optional Arguments')

# To add arguments to the relevant category, use syntax like:
required_pos.add_argument('dataset', help='path to dataset')
required_nam.add_argument('-o', '--outfile', required=True, 
                          help='path to output file')
optional.add_argument('-t', '--threads', type=int, default=10,
                      help='number of CPUs to use. [default: %(default)s]')

# This creates help messages like:
Required Positional Arguments:
  dataset               path to dataset

Required Named Arguments:
  -o OUTFILE, --outdir OUTFILE
                        path to output file

Optional Arguments:
  -t THREADS, --threads THREADS
                        number of CPUs to use. [default: 10]
Comment

PREVIOUS NEXT
Code Example
Python :: count consecutive values in python 
Python :: python execute function from string 
Python :: geopandas legend location 
Python :: plt text matplotlib white background 
Python :: merge multiple excel workssheets into a single dataframe 
Python :: Active Voice Python 
Python :: creating a sqlite3 table in python and inserting data in it 
Python :: python string format 
Python :: how to find the speed of a python program 
Python :: merging df vertically 
Python :: create a superuser to access django admin 
Python :: django get parameters from url 
Python :: how to print upto 5 decimal places in python 
Python :: create pyspark dataframe from list 
Python :: import ImageGrab 
Python :: django textfield 
Python :: how to check if there is a word in a string in python 
Python :: install pythonjsonlogger 
Python :: how to use input in python 
Python :: numpy array unique value counts 
Python :: replace word in column pandas lambda 
Python :: zip multiple lists 
Python :: planet 
Python :: count elements in columns pandas 
Python :: enter selenium in python 
Python :: import all csv python 
Python :: how to redirect in django 
Python :: python request coinmarketcap 
Python :: add a list in python 
Python :: how to check a phone number is valid in python 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =