clustershell
clustershell copied to clipboard
migrate to argparse
Migrate to argparse which is more powerful and would provide an easy way to create tools based on the clustershell library. Will also help for Python 3 support (#239).
Usefulness:
- easily implement tools that have default command line options:
-w
/-x
,-b
, ... (#295) - allow to specify an optional value for a specific argument just using
nargs='?'
(https://docs.python.org/2/library/argparse.html#nargs), will be useful for--pick
Official argparse doc pages:
- https://docs.python.org/2/library/argparse.html
- https://docs.python.org/3/library/argparse.html
Tutorial:
- https://docs.python.org/2/howto/argparse.html
Example of argparse implementation I like:
- https://github.com/google/oauth2client/blob/master/oauth2client/tools.py
Here argparser
created in this tools
module actually enables scripts to easily inherit oauth2 options:
from oauth2client import tools
def create_parser():
parser = argparse.ArgumentParser(add_help=False)
# add scripts custom options
#parser.add_argument(...)
return argparse.ArgumentParser(parents=[tools.argparser, parser])
The idea is to create a argparser per group of command line options. The obvious one is for manipulating node sets (-w
/-x
etc.).
Note: Before Python 2.7/3.2, python-argparse will become a new dependency, but it is easily available (even available in EPEL5, just saying :)).
Why not go with click instead? Have been using this for a while and it's so much nicer then argparse and friends.
Thanks @asteven for the suggestion. I never used click before. I tried to migrate clustershell's cli to argparse with no functional changes but it's tough.
I'm currently more interested in ClusterShell as a library then the cli tools. But if you happen to like click and decide to go with it I may be able to contribute. Thanks for Python3 support btw. Much appreciated.