1

I am trying to add a feature to my program where the user can interact with the program to run specific functions. But the best I can think of is this:

while input != "Exit" and input != "Quit":
    if input() == 'command' + arguments:
        Do X
    elseif input() == 'Othercommand' + arguments:
        Do Y

This doesn't sound like a good solution though.

This program will only be used by me so it doesn't need to be secure in terms of input checks. It is not really big enough project to use a GUI either (though I have done that before).

The program is going to be a webscraper where I can output the data as a JSON file, and read lists to go and scrape different websites, so this will have different functions like read file, save file, update data, etc. I am not sure whether this is the best idea for this type of program so any suggestions about different ideas would be welcome.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • use argparse. https://docs.python.org/3/library/argparse.html – Adithya Shetty Dec 17 '20 at 03:01
  • this may help https://docs.python-guide.org/scenarios/cli/ – sahasrara62 Dec 17 '20 at 03:09
  • Does this answer your question? [how to make a Command Line Interface or Interpreter in python](https://stackoverflow.com/questions/3910160/how-to-make-a-command-line-interface-or-interpreter-in-python) – mkrieger1 Dec 24 '20 at 23:27
  • Does this answer your question? [Implementing a "\[command\] \[action\] \[parameter\]" style command-line interfaces?](https://stackoverflow.com/questions/362426/implementing-a-command-action-parameter-style-command-line-interfaces) – Gino Mempin Dec 24 '20 at 23:33

2 Answers2

1

If you want to have a proper command-line interface for your application, the argparse is the module to go.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--name', dest='name', type=str, help='Name of the candidate')
parser.add_argument('--surname', dest='surname', type=str, help='Surname of the candidate')
parser.add_argument('--age', dest='age', type=int, help='Age of the candidate')

args = parser.parse_args()
print(args.name)
print(args.surname)
print(args.age)

Kickstart tutorial

Sivaram Rasathurai
  • 5,533
  • 3
  • 22
  • 45
-2

The CMD Python Module allows for a User to input multiple commands to a python program.