9

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
  • You say the argparse parents=[parser1, parser2] functionality hasn't worked for you - how does it break? Commented Mar 19, 2013 at 8:45

1 Answer 1

13

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...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.