willyg302 / clip.py Goto Github PK
View Code? Open in Web Editor NEWEmbeddable, composable [c]ommand [l]ine [i]nterface [p]arsing
License: MIT License
Embeddable, composable [c]ommand [l]ine [i]nterface [p]arsing
License: MIT License
It would be a nice to have a way to dump/list all the sub-commands as a tree. My current use has ~50+ leafnodes, my users are getting lost and they'd like some sort of overview.
Would be useful to allow an option to appear several times, like:
-H x=y -H a=b
Subcommands should be displayed in alphabetical order on help screens.
Any chance of a side-by-side comparison with click (http://click.pocoo.org/3/)? Why use clip.py instead of click?
Thanks!
import clip
def print_version():
print('Version 0.0.0')
clip.exit()
@clip.app(description='Make for stupid people')
@clip.flag('--version', callback=print_version, help='Print the version')
@clip.flag('-s', '--silent', global=False)
def app():
pass
@app.subcommand(help='Clone a project and run its install task')
@app.arg('source', help='The directory or repo to clone from')
@app.opt('-d', '--dest', help='Where to initialize the project')
@app.inherit(['-s'])
def init(source, dest, silent=False):
# @TODO
pass
@app.subcommand(help='Run one or more tasks defined in a project\'s strapme file')
@app.arg('tasks', nargs='*', default=['default'], help='The task(s) to run')
@app.opt('-d', '--dir', default=os.getcwd(), help='Optional path to execute the tasks from')
@app.inherit(['-s'])
def run(tasks, dir, silent=False):
# @TODO
pass
@app.subcommand(help='Manage strap\'s dependency cache')
def cache(action):
# @TODO
pass
@cache.subcommand()
def clean():
# @TODO
pass
@cache.subcommand(name='list')
def cache_list():
# @TODO
pass
@app.subcommand(name='list', help='List the tasks defined in a project\'s strapme file')
def list_tasks():
# @TODO
pass
def main():
if len(sys.argv) == 1:
sys.argv.append('run') # Run default task
try:
clip.run()
except clip.ClipExit:
pass
if __name__ == '__main__':
main()
Produces:
$ strap --help
strap: Make for stupid people
Usage: strap [-h] [--version] {subcommand}
Options:
-h, --help Show this help message and exit
--version Print the version
Subcommands:
init Clone a project and run its install task
run Run one or more tasks defined in a project's strapme file
cache Manage strap's dependency cache
list List the tasks defined in a project's strapme file
$ strap init -h
strap init
Usage: init [-h] [-d=DEST] [-s] <source>
Arguments:
source The directory or repo to clone from
Options:
-h, --help Show this help message and exit
-d, --dest=DEST Where to initialize the project
-s, --silent
$ strap run -h
strap run
Usage: run [-h] [-d=DIR] [-s] [<tasks>...]
Arguments:
tasks The task(s) to run
Options:
-h, --help Show this help message and exit
-d, --dir=DIR Optional path to execute the tasks from (default: ./)
-s, --silent
$ strap cache -h
strap cache
Usage: cache [-h] {subcommand}
Options:
-h, --help Show this help message and exit
Subcommands:
clean
list
$ strap list -h
strap list
Usage: list [-h]
Options:
-h, --help Show this help message and exit
$ strap cache clean -h
strap cache clean
Usage: clean [-h]
Options:
-h, --help Show this help message and exit
$ strap cache list -h
strap cache list
Usage: list [-h]
Options:
-h, --help Show this help message and exit
import clip
@clip.app(description='Echoes words to the command line', epilogue='Have fun echoing!')
@clip.arg('words', nargs='*')
def echo(words):
print(' '.join(words))
try:
clip.run()
except clip.ClipExit:
pass
Produces:
$ python echo.py -h
echo: Echoes words to the command line
Usage: echo [-h] [<words>...]
Arguments:
words
Options:
-h, --help Show this help message and exit
Have fun echoing!
$ python echo.py this is cool
this is cool
For example:
import clip
def ab(f):
f = clip.opt('-b')(f)
f = clip.arg('a')(f)
return f
app = clip.App()
@app.main()
@clip.opt('-c')
@ab
@clip.opt('-d')
def main(c, a, b, d):
print c, a, b, d
if __name__ == '__main__':
try:
app.run()
except clip.ClipExit:
pass
These utility decorators would take a function, add several clip args/opts to it, and return it. One real-world example is a @time
decorator that would apply start, end, and duration arguments to a command. Because composability!
Hello!
Due to the ClipGlobals
class, the stdout
and sterr
can only be specified once for multiple App
s in the same Python run.
By this, I mean that if you have multiple App
objects (a different one for each embedded use, etc) the stdout
and stderr
are set to value specified by the last instantiated App
.
For example, in this code:
first_app = clip.App(stdout=first_stream)
# Later on...
second_app = clip.App(stdout=second_stream)
If I were to clip.echo()
in a command in first_app
, it would write to second_stream
.
It may take some significant refactoring to fix this due to the structure of the project. If too large to fix, any workarounds available?
I'd like my top-level (main) command to do nothing, and if called simply output the help text. I didn't find that use case in the docs, and I couldn't intuitively tell from the source how to go about it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.