Click Click - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: Click Click (/thread-2310.html) |
Click Click - snippsat - Mar-07-2017 So this is about Click,getting stuff from command line into Python. There are many existing solution for this,i would say that Click is the best of the bunch All code testes om Win-10(Python 36) and Linux Mint 18.1(Python 35). Shell cmder Win-10 and default shell Mint One of the most powerful feature of Click is click.echo() It solve Unicode from command line on all OS,also Windows console which is just impressive. So first example gone put it trough a hard Unicode test. # click_unicode.py import click @click.command() @click.option('--count', default=3, help='Number of greetings.') @click.option('--name', prompt='Your name', help='Test of difficult Unicode') def hello(count, name): """Simple program that greets NAME for a total of COUNT times.""" for x in range(count): click.echo(f'Hello {name} {"☂"}') #36 #click.echo('Hello {} {}'.format(name, "☂")) #35 if __name__ == '__main__': hello()From command line: So it work the same on Win and Linux.Setting prompt it work like Python's input() . Setting count will repeat prompt 3 times.A look getting different types form command line. Here a look at float type:# float_test.py import click @click.command() @click.option('--pos', nargs=2, type=float) def get_float(pos): n1,n2 = pos total = n1 / n2 click.echo(f'{n1} / {n2} = {total:.2f}') #36 #click.echo('{} / {} = {:.2f}'.format(n1,n2,total)) #35 if __name__ == '__main__': get_float()From command line: So nargs how many argumet that is wanted an that type is float .Getting a tuple(pos) back unpack and use it to calculate. Mix types in this example string("str"which is Unicode python 3) and integer. # str_int.py import click @click.command() @click.option('--item', type=(str, int)) def mix(item): #name,number = item click.echo('Name is <{}> and number is: <{}>'.format(*item)) #35 #click.echo(f'Name is <{name}> and number is: <{number}>') #36 if __name__ == '__main__': mix()From command line:
Prompt same as input() python. import click @click.command() @click.option('--name', prompt=True) # prompt='Your name please' def hello(name): click.echo(f'Hello {name}') #36 #click.echo('Hello {}'.format(name)) #35 if __name__ == '__main__': hello()From command line:
Getting a file. # file_arg.py import click @click.command() @click.argument('f', type=click.Path(exists=True)) def touch(f): file_name = click.format_filename(f) with open(file_name) as f: print(f'Content of {file_name} is:\n{f.read()}') #36 #print('Content of {} is:\n{}'.format(file_name, f.read())) #35 if __name__ == '__main__': touch() So click.Path(exists=True) dos error cheeking.When okay use with open() and read the file. I may add more to this demo RE: Click Click - wavic - Mar-07-2017 This is very nice resume. Useful too. I've tried click before but just to see how it works. Nothing more. Which modules have you tried? Have you seen prompt_toolkit? Perhaps it's too powerful for little projects. I don't know it handle Unicode. RE: Click Click - snippsat - Mar-07-2017 Quote:Which modules have you tried?I have tested some from standard library(getopt, optparse and argparse) where argparse is the best of theme. sys.argv(simple list of arguments) which many has a first encounter with this topic. Tested Docopt,not prompt_toolkit. I use ptpython as REPL,where prompt_toolkit had and important part in making of ptpython. I someone has tested something cool on this topic feel free to post here. Python Fire is a new one,this is something that Google has used internal which is now open source. RE: Click Click - snippsat - Mar-10-2017 A quick look at Python Fire. It's kind of cool,it simplify it down bye using some magic. I want to start with the class and use it in a normal way. class Calculator: """A simple calculator class.""" def double(self, number): return 2 * number if __name__ == '__main__': obj = Calculator() print(obj.double(5)) print(obj.double(4.6)) print(obj.double('hello')) So the normal way,instance of the class and assigns to variable obj.Use method object "obj" and call method double. Now the same using fire. # fire_class.py import fire class Calculator: """A simple calculator class.""" def double(self, number): return 2 * number if __name__ == '__main__': fire.Fire(Calculator)From command line: So file name fire_class.py is acting like the method object "obj".So we can use the class in same way as in first example. Using a function. # fire_func.py import fire def hello(name): return f'Hello {name}!☂' if __name__ == '__main__': fire.Fire(hello)From command line: So will *args work so we can give many argument in # fire_arg.py import fire def hello(*args): return f'Hello {args}!☂' if __name__ == '__main__': fire.Fire(hello)From command line:
Other stuff eg --interactive From command line: So it start a IPython interactive shell.
RE: Click Click - GamingAgent - Apr-15-2017 cool! RE: Click Click - wavic - Apr-16-2017 I know Fire. Good idea However, I want to know, how to get positional arguments with Click? RE: Click Click - snippsat - Apr-16-2017 (Apr-16-2017, 07:28 AM)wavic Wrote: However, I want to know, how to get positional arguments with Click?Something like this. If nargs is set -1,then an unlimited number of arguments is accepted. #numb_input.py import click @click.command() @click.argument('n', nargs=-1) def numbers(n): click.echo(n) click.echo(sum(int(i) for i in n)) if __name__ == '__main__': numbers() As a example sum first numbers,then make a dict of last argument.#numb_input_1.py import click @click.command() @click.argument('n', nargs=-1) @click.argument('d', nargs=1) def numbers(n, d): click.echo(sum(float(i) for i in n)) to_dict = dict([d.split('=')]) click.echo(to_dict) if __name__ == '__main__': numbers()
|