2017-07-07 33 views
1

我在10年内没有使用过argparse,但我理解它,以及我在下面的工作,因为我希望它能够工作......但是这将会获得很多随着我继续添加命令,解析器和子分析器而变得更加复杂。我想知道组织这个最好的方法是什么?在我看来,我应该能够看到文本中的命令序列,几乎和我在图表中看到的一样清晰......但是每次看到它,离开一段时间后,我的大脑会游泳,因为我试图遵循它。必须有更好的方式来组织这种权利? enter image description herePython Argparse,如何正确组织ArgParse代码

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. 解析器/子分析器重命名。
  2. 更改语句的顺序。
  3. 某种方式来缩进而不与python混淆。

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

+0

我会改变的订购和使用较短的变量名。一旦创建完成,就向分析器添加参数,并为分析器使用简短的通用名称。然后,您可以为每个子分析器重用相同的短名称。 – chepner

回答

0

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

是否有任何适用于您的情况,我不知道。你的代码是可读的,所以我很容易知道你在做什么。我从来没有见过任何人使用这么多级别的子分析器。事实上,使用多个关卡是一件新奇的事情(从一些以前的问题来看)。

我想强调的是​​的第一份工作就是弄清楚你的用户想要什么。解析是第一要务。其次,它可以很容易地表达他们想要的东西。我想知道这个多级子分析是否容易使用。​​在子分析器中拆分help的方式使得很难显示大概述。

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

您可能会收到关于CodeReview的更好回答。那里的常客似乎对代码组织和命名问题更感兴趣。 SO更倾向于解决问题。如果话题过于专业化,我不鼓励转介到CR,但这是一个比特定问题更为普遍的Python问题。但请阅读CR对问题的期望。

Multiple level argparse subparsers

Argparse with required subparser

argparse subparser monolithic help output

Python argparser repeat subparse

+0

感谢您的信息...将有助于看到你如何重组这个......我绝对是封装在函数中的游戏,但不确定使用这个包的最佳方法。至于制作这样的多个lvl命令,我会考虑zfs和zpool命令,因为我设计了这个命令...... argparse似乎使事情变得比需要的复杂,一个自定义的解析器将会非常棒!唯一的问题是,我不知道如何将main-command/python-file-name之后的所有内容都放到一个变量中,以便我可以自定义解析它。 – gunslingor