python命令行参数argparse的简单使用

1、终端中执行脚本程序

  • pycharm的终端中执行  python xxx.py
  • 命令行中执行程序

 2、获取命令行输入的参数

import sys

print(sys.argv)

 

3.专门处理命令行的library:argparse 

  • 添加optional arguments参数:默认是可选的,意味着可以不用填写
parser.add_argument("--b",type=int,default=6,help="操作数 B")
  • 添加positional arguments参数:默认是不可选的,必须填写,否则报错
    parser.add_argument("method",type=str,help="方法")
  •  添加flags,标记,开关那种形式
    比如说,添加一个参数,是否需要打印信息,--verbose表示打印详细信息
    parser.add_argument("--verbose",action="store_true",help="Print Message")
    import sys
    import argparse
    
    # print(sys.argv)
    
    # 1.先创建解释器
    parser=argparse.ArgumentParser(description="解析命令行")
    
    # 2.添加参数
    parser.add_argument("--a",type=int,default=5,help="操作数 A")
    parser.add_argument("--b",type=int,default=6,help="操作数 B")
    parser.add_argument("method",type=str,help="方法")
    parser.add_argument("--verbose",action="store_true",help="Print Message")
    
    # 3.解析命令行
    args = parser.parse_args()
    
    print(args)
    print(args.a,type(args.b))

    4.返回对象的属性和值的字典形式

vars()是Python的一个内置函数,用于返回对象的属性和值的字典形式。

当你传递一个对象作为参数给vars()函数时,它会返回该对象的字典表示。字典的键是对象的属性名称,而对应的值是属性的值。

from argparse import Namespace

args = Namespace(auto_boundary=True, boundary={}, build_overviews=False, camera_lens='auto', cameras={}, cog=False,
                 copy_to=None, crop=3, debug=False, dem_decimation=1, dem_euclidean_map=False, dem_gapfill_steps=3,
                 dem_resolution=5, depthmap_resolution=640, dsm=False, dtm=False, end_with='odm_postprocess',
                 fast_orthophoto=True, feature_quality='high', feature_type='sift', force_gps=True, gcp=None, geo=None,
                 gps_accuracy=0.0001, ignore_gsd=False, matcher_neighbors=0, matcher_type='flann', max_concurrency=12,
                 merge='all', mesh_octree_depth=11, mesh_size=200000, min_num_features=10000, name='code',
                 name_is_set=True,
                 no_gpu=False, optimize_disk_space=False, orthophoto_compression='DEFLATE', orthophoto_cutline=False,
                 orthophoto_kmz=False, orthophoto_no_tiled=False, orthophoto_png=False, orthophoto_resolution=5,
                 pc_classify=False, pc_copc=False, pc_csv=False, pc_ept=False, pc_filter=2.5, pc_geometric=False,
                 pc_las=False, pc_quality='medium', pc_rectify=False, pc_sample=0, pc_tile=False, primary_band='auto',
                 project_path='/', radiometric_calibration='none', rerun=None, rerun_all=False, rerun_from=None,
                 resize_to=4000, rolling_shutter=False, rolling_shutter_readout=0, sfm_algorithm='incremental',
                 skip_3dmodel=True, skip_band_alignment=False, skip_orthophoto=False, skip_report=False,
                 sm_cluster=None, smrf_scalar=1.25, smrf_slope=0.15, smrf_threshold=0.5, smrf_window=18.0, split=999999,
                 split_image_groups=None, split_overlap=150, texturing_data_term='gmi',
                 texturing_keep_unseen_faces=False, texturing_outlier_removal_type='gauss_clamping',
                 texturing_skip_global_seam_leveling=False, texturing_skip_local_seam_leveling=False,
                 texturing_tone_mapping='none', tiles=False, time=False, use_3dmesh=False, use_exif=True,
                 use_fixed_camera_params=False, use_hybrid_bundle_adjustment=False, verbose=False,
                 **{'3d_tiles': False})
args_dict = vars(args)
print(args_dict)

猜你喜欢

转载自blog.csdn.net/Scarlett2025/article/details/132297004
今日推荐