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

argparse print help if no arguments

parser.argparse.ArgumentParser()
# parser.add_args here

# sys.argv includes a list of elements starting with the program
if len(sys.argv) < 2:
    parser.print_usage()
    sys.exit(1)
Comment

PREVIOUS NEXT
Code Example
Python :: python docstring example 
Python :: Clear All the Chat in Discord Channel With Bot Python COde 
Python :: color name to hex python 
Python :: numpy add new column 
Python :: remove punctuation python string library 
Python :: python iterate through files 
Python :: tkinter entry 
Python :: print class python 
Python :: check if a the time is 24 hours older python 
Python :: PYTHON 3.0 MAKE A HEART 
Python :: opencv invert image 
Python :: list files python 
Python :: connect spark to postgres; connect spark to database 
Python :: plt.imread python 
Python :: how to reverse a list in python without using inbuilt function 
Python :: how to run cmd line commands in python 
Python :: how to clear ipython console 
Python :: How to perform Bubble sort in Python? 
Python :: how to get current latitude and longitude in python 
Python :: python kill all threads 
Python :: how to import turtle in python 
Python :: how to clear a list in python 
Python :: anaconda 3 geopandas 
Python :: pattern in python 
Python :: python time function in for loop 
Python :: mss python install 
Python :: dictionary size in python 
Python :: python create file if doesnt exist 
Python :: how to remove tkinter icon 
Python :: python input timeout 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =