Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

argparse required arguments

parser.add_argument('--use-lang', required=True, help="Output language")
Comment

python argparse optional 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 :: python integer to string 
Python :: Discord python get member object by id 
Python :: make button bigger tkinter with grid 
Python :: font in tkinter 
Python :: how to assign a new value in a column in pandas dataframe 
Python :: Aligning rotated xticklabels with their respective xticks 
Python :: python array methods 
Python :: list of dataframe to dataframe 
Python :: ord python 
Python :: python datetime compare date 
Python :: subarray in python 
Python :: file manage py line 17 from exc django 
Python :: raku fibonacci 
Python :: compile python folder 
Python :: get the time of 1 minute later in python 
Python :: delete element list python 
Python :: check if string contains python 
Python :: python loop through dictionary 
Python :: transition from python 2 to 3 terminal 
Python :: combination without repetition python 
Python :: pandas melt() function, change the DataFrame format from wide to long 
Python :: python regex search file 
Python :: planets python 
Python :: drop row with condition dataframe 
Python :: python remove many items via index at oncefrom a list? 
Python :: print with no newline 
Python :: how to call a random function in python 
Python :: circumference of a circle python 
Python :: keras maxpooling1d 
Python :: merge all mp4 video files into one file python 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =