Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Click Click
#1
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 Thumbs Up

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:
Output:
λ python click_unicode.py Your name: chess☺ king♟♜♞ Hello chess☺ king♟♜♞ ☂ Hello chess☺ king♟♜♞ ☂ Hello chess☺ king♟♜♞ ☂ λ python click_unicode.py --help Usage: click_unicode.py [OPTIONS]  Simple program that greets NAME for a total of COUNT times. Options:  --count INTEGER  Number of greetings.  --name TEXT      Test of difficult Unicode  --help           Show this message and exit.
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:
Output:
λ python float_test.py --pos 14 3 14.0 / 3.0 = 4.67 λ python float_test.py --pos 14 3 100 Usage: float_test.py [OPTIONS] Error: Got unexpected extra argument (100)
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:
Output:
λ python str_int.py --item †øღ 12345 Name is <†øღ> and number is: <12345> λ python str_int.py --item †øღ 12345 world Usage: str_int.py [OPTIONS] Error: Got unexpected extra argument (world)


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:
Output:
λ python prompt.py Name: Ħαηṧ Hello Ħαηṧ


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()
Output:
λ python file_arg.py aaa.txt Usage: file_arg.py [OPTIONS] F Error: Invalid value for "f": Path "aaa.txt" does not exist. E:\1py_div\click λ python file_arg.py hello.txt Content of hello.txt is: hello world
So click.Path(exists=True) dos error cheeking.
When okay use with open() and read the file.

I may add more to this demo Cool
Reply


Messages In This Thread
Click Click - by snippsat - Mar-07-2017, 12:04 AM
RE: Click Click - by wavic - Mar-07-2017, 06:17 AM
RE: Click Click - by snippsat - Mar-07-2017, 12:34 PM
RE: Click Click - by snippsat - Mar-10-2017, 07:52 PM
RE: Click Click - by GamingAgent - Apr-15-2017, 09:00 AM
RE: Click Click - by wavic - Apr-16-2017, 07:28 AM
RE: Click Click - by snippsat - Apr-16-2017, 12:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  wxpython single click/double click recognition Larz60+ 1 6,223 Jul-23-2017, 06:49 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020