Working on a script to make launching lxc containers more flexible; need better help according to a test user :)
#!/usr/bin/env python3
import argparse
import sys
def parse_args():
parser = argparse.ArgumentParser(description="stand up an lxc container")
if len(sys.argv) == 1:
parser.format_help()
parser.add_argument("-4i", "--fouri", type=str, help="IPv4 address, if containername NOT in DNS (yet)")
parser.add_argument("-6i", "--sixi", nargs='?', const=1, default="::2", type=str, help="IPv6 address, if containername NOT in DNS (yet)")
parser.add_argument("-4m", "--fourm", nargs='?', const=1, default="24", type=str, help="IPv4 netmask, if unset '24'")
parser.add_argument("-6m", "--sixm", nargs='?', const=1, default="64", type=str, help="IPv6 netmask, if unset '64'")
parser.add_argument("-4g", "--fourg", type=str, help="IPv4 gateway")
parser.add_argument("-6g", "--sixg", nargs='?', const=1, default="::1", type=str, help="IPv6 gateway")
parser.add_argument("-i", type=str, required=True, help="name of the image to launch from")
parser.add_argument("-c", type=str, required=True, help="hostname of the container to be launched")
return parser.parse_known_args()
def main():
args, unknown = parse_args()
if __name__ == "__main__":
main()
Here's what I get if I invoke the script w/o any arguments:
./test.py
usage: test.py [-h] [-4i FOURI] [-6i [SIXI]] [-4m [FOURM]] [-6m [SIXM]]
[-4g FOURG] [-6g [SIXG]] -i I -c C
test.py: error: the following arguments are required: -i, -c
I'd like to get the same output I'd get if I invoked it with --help / -h:
./test.py -h
usage: test.py [-h] [-4i FOURI] [-6i [SIXI]] [-4m [FOURM]] [-6m [SIXM]]
[-4g FOURG] [-6g [SIXG]] -i I -c C
stand up an lxc container
optional arguments:
-h, --help show this help message and exit
-4i FOURI, --fouri FOURI
IPv4 address, if containername NOT in DNS (yet)
-6i [SIXI], --sixi [SIXI]
IPv6 address, if containername NOT in DNS (yet)
-4m [FOURM], --fourm [FOURM]
IPv4 netmask, if unset '24'
-6m [SIXM], --sixm [SIXM]
IPv6 netmask, if unset '64'
-4g FOURG, --fourg FOURG
IPv4 gateway
-6g [SIXG], --sixg [SIXG]
IPv6 gateway
-i I name of the image to launch from
-c C hostname of the container to be launched
You can override the ArgumentParser.print_usage method with the ArgumentParser.print_help method:
def parse_args():
parser = argparse.ArgumentParser(description="stand up an lxc container")
parser.print_usage = parser.print_help
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With