Using the argparse output to call functions

前端 未结 3 1151
北恋
北恋 2021-02-14 06:39

Currently my code looks like this. It allows me to parse multiple parameters my program script gets. Is there a different way that is closer to \'best practices\'? I haven\'t se

3条回答
  •  广开言路
    2021-02-14 06:47

    You could supply a custom action for an argument by, and I quote:

    passing an object that implements the Action API. The easiest way to do this is to extend argparse.Action, supplying an appropriate __call__ method. The __call__ method should accept four parameters:

    1. parser: The ArgumentParser object which contains this action.
    2. namespace: The namespace object that will be returned by parse_args(). Most actions add an attribute to this object.
    3. values: The associated command-line args, with any type-conversions applied.(Type-conversions are specified with the type keyword argument to add_argument().
    4. option_string: The option string that was used to invoke this action. The option_string argument is optional, and will be absent if the action is associated with a positional argument.

提交回复
热议问题