Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file with option
#1
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
Reply
#2
The argparse module gives you facilities for adding command line arguments.
Reply
#3
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
    ''',
)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  logger option , where is the file? korenron 1 1,795 Apr-25-2021, 01:28 PM
Last Post: snippsat
  Need help implmenting if/else or case statements for option to choose file format. samlee916 1 2,021 Jul-22-2020, 06:06 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