sum sum the integers (default: find the max)Įxample of calculating maximum value : python prog.py 1 2 3 4Įxample of summation : python prog.py 1 2 3 4 -sumĪn example of passing in an invalid parameter and reporting an error : python prog.py a b cĮrror message : usage: prog.py N
h, -help show this help message and exit Run on the command line python prog.py -h, Get prompt information : usage: prog.py N
add_argument ( '-sum', dest = 'accumulate', action = 'store_const' ,Ĭonst = sum, default = max, help = 'sum the integers (default: find the max)' ) #integers Property will be a list of one or more integers #accumulate Property when specified on the command line -sum Parameter will be sum() function, Otherwise it would be max() function. add_argument ( 'integers', metavar = 'N', type = int, nargs = '+', help = 'an integer for the accumulator' ) ArgumentParser (description = 'Process some integers.' ) #description Is the text displayed before the parameter help document The following examples and the overall content architecture refer to the official documents :Įxample prog.py( Get a list, And calculate its sum or maximum ): import argparse Run the following statement on the command line : python example.py -section_id 2 -gpu_id 1(example.py by Python Script, The parameter is followed by the value to be passed in )īe careful, If you use anaconda As the environment manager, If you want to use python command, Need to make terminal In a virtual environment Otherwise, it will be used Python The interpreter's path replaces python command. _dict_ # take arg_dict Convert to dict Formatĭevice = torch. parse_args ( ) # Analytical parametersĪrg_dict =args. The following text will give a detailed introduction to each participantĪrgs = parser. add_argument ( "-gpu_id" ,default = '0', type = str ) # Add parameter. add_argument ( "-section_id" ,default = '1', type = str ) ArgumentParser ( ) # Create a parser #ArgumentParser Object contains the command line parsed into Python All the information required for the data type. Ī simple application example : Use argparse Pass in the parameter section_id and gpu_id, And get two parameters ( among gpu_id Go straight to vice 1) parser = argparse. This article is only for argparse A brief introduction to the necessary functions of the package, Its deeper application may continue to be updated later.
Argparse python 3 install code#
This paper will first give the code format that can be applied directly, Then on this basis, we will introduce more content. On the command line Python Pass parameters directly during script. Īrgparse modular (Python Official documents : argparse - Command line options 、 Parameter and subcommand parsers - Python 3.10.2 file ), Can be used to run.
VX Number “PolarisRisingWar” You can directly search and add author friends to discuss.