Basic intended usage:
my_framework create Project_title /path/to/project
OR
my_framework create Project_title
(ie. use current working directory)
OR
my_framework update
(ie. update my_framework rather than creating a new project)
I know I can make name
optional by providing it with a default, but, in reality name
is not optional provided the user has entered create
as the first argument.
Best solution I've come up with is to use a default value for name
and then, if the argument name
equals its default value, throw an error. But if there's a way to make argparse do this work for me I'd rather learn to do it.
Writing two scripts, my_framework_create
and my_framework_update
doesn't appeal to me aesthetically.
#!/usr/bin/env python
import argparse
import os
import shutil
from subprocess import call
template_path = "/usr/local/klibs/template"
parser = argparse.ArgumentParser("MY_FRAMEWORK CLI", description='Creates a new MY_FRAMEWORK project or updates MY_FRAMEWORK')
parser.add_argument('action', choices=['create', 'update'], type=str, help='<help text>')
parser.add_argument('name', type=str, help='<help text>')
parser.add_argument('path', default=os.getcwd(), nargs="?", type=str, help='<help text>')
args = parser.parse_args()
if args.action == "create":
# do the create stuff
if args.action == "update":
# do the update stuff
The best way to do this is with a subparser
An example from the docs:
In your case you would have
create
andupdate
as separate subparsers.Example: