Using python's argparse in multiple scripts ba

2019-04-28 16:26发布

I'm building out a set of scripts and modules for managing our infrastructure. To keep things organized I'd like to consolidate as much effort as possible and minimize boiler plate for newer scripts.

In particular the issue here is to consolidate the ArgumentParser module.

An example structure is to have scripts and libraries organized something like this:

|-- bin
    |-- script1
    |-- script2
|-- lib
    |-- logger
    |-- lib1
    |-- lib2

In this scenario script1 may only make use of logger and lib1, while script2 would make use of logger and lib2. In both cases I want the logger to accept '-v' and '-d', while script1 may also accept additional args and lib2 other args. I'm aware this can lead to collisions and will manage that manually.

script1

#!/usr/bin/env python
import logger
import lib1
argp = argparse.ArgumentParser("logger", parent=[logger.argp])

script2

#!/usr/bin/env python
import logger
import lib2

logger

#!/usr/bin/env python
import argparse
argp = argparse.ArgumentParser("logger")
argp.add_argument('-v', '--verbose', action="store_true", help="Verbose output")
argp.add_argument('-d', '--debug', action="store_true", help="Debug output. Assumes verbose output.")

Each script and lib could potentially have it's own arguments, however these would all have to be consolidated into one final arg_parse()

My attempts so far have resulted in failing to inherit/extend the argp setup. How can this be done in between a library file and the script?

1条回答
姐就是有狂的资本
2楼-- · 2019-04-28 16:52

The simplest way would be by having a method in each module that accepts an ArgumentParser instance and adds its own arguments to it.

# logger.py
def add_arguments(parser):
    parser.add_argument('-v', '--verbose', action="store_true", help="Verbose output")
    # ...

# lib1.py
def add_arguments(parser):
    parser.add_argument('-x', '--xtreme', action="store_true", help="Extremify")
    # ...

Each script would create their ArgumentParser, pass it in to each module that provides arguments, and then add its own specific arguments.

# script1.py
import argparse, logger, lib1
parser = argparse.ArgumentParser("script1")
logger.add_arguments(parser)
lib1.add_arguments(parser)
# add own args...
查看更多
登录 后发表回答