Python Argparse,如何正确组织Argparse Cod

2024-09-27 21:32:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经有10年没有使用argparse了,但我理解它,我下面的内容确实可以像我希望的那样工作。。。但随着我继续添加命令、解析器和子parser,这将变得更加复杂。我想知道什么是最好的组织方式?在我看来,我应该能够清楚地看到文本中的命令序列,就像我在图表中看到的一样。。。但每次我看到它,在离开一段时间后,我的大脑会随着我试图跟随它而游动。一定有更好的方法来组织这件事吧?enter image description here

import argparse
from modules import cli_tools

#LVL 1: create the top-level parser for the "sacs" command.
sacs_parser = argparse.ArgumentParser(prog = 'sacs', description = 'Master Command For Controlling SACS.')
sacs_subparsers = sacs_parser.add_subparsers(help='Management Module Selector.')

#LVL 2: create the second-level parsers for the "sacs [module]" commands.
csv_parser = sacs_subparsers.add_parser('csv', help='Generic CSV Management Module.')
am_parser = sacs_subparsers.add_parser('am', help='SACS Asset Management Module.')
mm_parser = sacs_subparsers.add_parser('mm', help='SACS Metric Management Module.')

#LVL 3: create the third-level subparser for the "sacs [module] [action]" commands.
csv_action_subparser = csv_parser.add_subparsers(help='The action to perform.')
mm_action_subparser = mm_parser.add_subparsers(help='The action to perform.')

#LVL 4: create the fourth-level subparser for the "sacs [module] [action] [type]" commands.
mm_create_parser = mm_action_subparser.add_parser('create', help='Used to Create a new event/asset input file.')
mm_create_type_parser = mm_create_parser.add_subparsers(help='The type of file to create.')

#LVL 5: create the fifth-level parser for the "sacs [module] [action] [type]" commands.
csv_reconcile_parser = csv_action_subparser.add_parser('reconcile', help='reconcile two csvs.')
mm_create_asset_parser = mm_create_type_parser.add_parser('assets', help='Create an Asset File.')
mm_create_asset_subtype_parser = mm_create_asset_parser.add_subparsers(help='The type of file to create.')
mm_create_event_parser = mm_create_type_parser.add_parser('events', help='Create an Event File.')

#LVL 6: create the sixth-level parser for the "sacs [module] [action] [type] [subtype]" commands.
mm_create_asset_uaid_parser = mm_create_asset_subtype_parser.add_parser('uaid', help='Create an Asset File with UAID as the primary key.')
mm_create_asset_vid_parser = mm_create_asset_subtype_parser.add_parser('vid', help='Create an Asset File with Vulnerability ID as the primary key.')

#COMMAND ARGS: Add Arguments to the final command "sacs csv reconcile [args]"
csv_reconcile_parser.add_argument('key', help='The name of the field that holds the unique ID to compare against.')
csv_reconcile_parser.add_argument('inputfile1', help='The master file (used when same record exists in both files).')
csv_reconcile_parser.add_argument('inputfile2', help='The secondary file, which is trumped by the master file.')
csv_reconcile_parser.add_argument('outputfile', help='The output file; note it will be overwritten if it exists.')
csv_reconcile_parser.set_defaults(func=cli_tools.csv_reconcile)

#COMMAND ARGS: Add Arguments to the final command "sacs mm create assets uaid [args]"
mm_create_asset_uaid_parser.add_argument('appmapp_file', help='The input file.')
mm_create_asset_uaid_parser.add_argument('output_file', help='The output file.')
mm_create_asset_uaid_parser.set_defaults(func=cli_tools.asset_create_uaid)

#COMMAND ARGS: Add Arguments to the final command "sacs mm create assets vid [args]"
mm_create_asset_vid_parser.add_argument('vulnerability_file', help='The input file.')
mm_create_asset_vid_parser.add_argument('appmapp_file', help='The output file.')
mm_create_asset_vid_parser.add_argument('output_file', help='The output file.')
mm_create_asset_vid_parser.set_defaults(func=cli_tools.asset_create_vid)

args = sacs_parser.parse_args()
args.func(args)

找到更好方法的潜在途径:

  1. 解析器/子parser重命名。在
  2. 更改语句的顺序。在
  3. 一些在不破坏python的情况下缩进的方法。在

所有的想法都摆在桌面上,我想看看其他人在设计复杂命令时如何处理这些问题。在


Tags: csvthetoaddparsercreatehelpaction
1条回答
网友
1楼 · 发布于 2024-09-27 21:32:54

增加Python代码清晰度的常用方法是将步骤打包到函数甚至类中。argparse本身就是一组类。每个解析器(包括子parser)都是argparse.ArgumentParser对象。每个add_argument创建一个argparse.Action子类对象。add_subparsers创建(并返回)一个专门的Action子类来处理子parser。最后parse_args返回一个argparse.Namespace对象。在

我不知道这些是否适用于你的情况。你的代码是可读的,所以我很容易就知道你在做什么。我从来没见过有人用这么多级别的助推器。事实上,使用多个级别是一种新奇的东西(从之前的一些SO问题来看)。在

我想强调的是,argparse的第一项工作是找出用户想要什么。解析是第一要务。其次,它应该使表达他们想要的东西变得容易。我想知道这种多级子划分是否易于使用。argparsehelp拆分到子parser中的方式使得很难显示一个大的概述。在

在某些时候,大软件包开始编写自己的帮助,甚至定制解析器以满足他们的需要。在

您可能会在CodeReview上得到更好的答案。那里的常客似乎对代码组织和命名问题更感兴趣。SO更倾向于解决问题。如果主题过于专业化,我不鼓励推荐CR,但这更像是一个普通的Python问题,而不是argparse特定的问题。但请仔细阅读CR对问题的期望。在

Multiple level argparse subparsers

Argparse with required subparser

argparse subparser monolithic help output

Python argparser repeat subparse

相关问题 更多 >

    热门问题