Python Forum

Full Version: file with option
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
H,
how to run a python file on the command line with a " - quick " option for example.
Indeed I have created two typ functions in my code to sort my array and I would like to choose according to the -quick or -heap option the function called to sort my array which is read a file.
thanks in advance.

Klo. Angel
The argparse module gives you facilities for adding command line arguments.
I have use Click for some years now.
Has some strong features that other tool in this category may lack.
Quick demo of sorting some input from command line.
import click

@click.command()
@click.option('-q', '--quick', type=str, help='Sort text input')
def my_sort(quick):
    lst = list(quick)
    lst = sorted(lst)
    click.echo(''.join(lst))

if __name__ == '__main__':
   my_sort()
Usage command line:
Output:
λ my_sort --quick 9593912358439 1233345589999 λ my_sort -q 94567phron230hdrt9931 012334567999dhhnoprrt λ my_sort --help Usage: my_sort [OPTIONS] Options: -q, --quick TEXT Sort text input --help Show this message and exit.
See that can call my_sort without python or .py
As Click has good Setuptools Integration.
from setuptools import setup

setup(
    name='my_sort',
    version='0.1',
    py_modules=['my_sort'],
    install_requires=[
        'Click',
    ],
    entry_points='''
        [console_scripts]
        my_sort=my_sort:my_sort
    ''',
)