Python tutorial explains it:

import sys

print(sys.argv)

More specifically, if you run python example.py one two three:

>>> import sys
>>> print(sys.argv)
['example.py', 'one', 'two', 'three']
Answer from SilentGhost on Stack Overflow
🌐
Tutorialspoint
tutorialspoint.com › python › python_command_line_arguments.htm
Python - Command-Line Arguments
The −help parameter is available by default. Now let us define an argument which is mandatory for the script to run and if not given script should throw error. Here we define argument 'user' by add_argument() method.
🌐
Python
docs.python.org › 3 › library › argparse.html
argparse — Parser for command-line options, arguments and subcommands
The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv. The argparse module also automatically generates help and usage messages. The module will also issue errors when users give the program invalid arguments.
🌐
W3Schools
w3schools.com › python › python_args_kwargs.asp
Python *args and **kwargs
Arbitrary Arguments are often shortened to *args in Python documentation.
🌐
Python
docs.python.org › 3.3 › library › argparse.html
16.4. argparse — Parser for command-line options, arguments and sub-commands — Python 3.3.7 documentation
The help value is a string containing a brief description of the argument. When a user requests help (usually by using -h or --help at the command line), these help descriptions will be displayed with each argument:
Top answer
1 of 2
1

The built-in argparse module as @ToTheMax said can create complex command line interfaces.

By default argparse.ArgumentParser.parse_args() will read the command line arguments to your utility from sys.argv, but if you pass in an array, it will use it instead.

You can lex (split into an array of "words") a string just like the shell is using shlex.split() which is also built in. If you use quotation marks like in your example, the words between them won't be split apart, just as in the shell.

Here's a complete example. Refer to the documentation, because this is a bit of an advance usage of argparse. There is a section that talks about "subcommands" which is what this example is based on.

import argparse
import shlex

def do_say(args):
    print(args.what)

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
say_command = subparsers.add_parser('say')
say_command.add_argument('what')
say_command.set_defaults(func=do_say)

command = 'say "hi this is a test"'

args = parser.parse_args(shlex.split(command))
args.func(args)

The cmd module is another built-in way to make a command prompt, but it doesn't do the parsing for you, so you'd maybe combine it with argparse and shlex.

2 of 2
1

I realize I already have a question that is answered.

You can find it here: How do you have an input statement with multiple arguments that are stored into a variable?

Here is the correct code:

def command_split(text:str) -> (str,str):
    """Split a string in a command and any optional arugments"""
    text = text.strip() # basic sanitize input
    space = text.find(' ')
    if space > 0:
        return text[:space],text[space+1:]
    return text,None

x = input(":>")
command,args = command_split(x)
# print (f'command: "{command:}", args: "{args}"')

if command == 'echo':
    if args == None:
        raise SyntaxError
    print (args)

A more simple way:

x = input(":>")
if x.split(" ")[0] == 'echo':
    echoreturn = ' '.join(x.split(" ")[1:])
    print(echoreturn)

My version to @rgov 's post: (Thank you!)

import argparse
import shlex

def do_say(args):
    print(args.what)

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
say_command = subparsers.add_parser('say')
say_command.add_argument('what')
say_command.set_defaults(func=do_say)

while True:
    try:

        command = input(":>")

        args = parser.parse_args(shlex.split(command))
        args.func(args)
    except SyntaxError:
        print("Syntax Error")
    except ValueError:
        print("Value Error")
    except:
        print("")
🌐
Real Python
realpython.com › python-command-line-arguments
Python Command-Line Arguments – Real Python
August 27, 2023 - It’s similar to what a graphical user interface is for a visual application that’s manipulated by graphical elements or widgets. Python exposes a mechanism to capture and extract your Python command-line arguments. These values can be used to modify the behavior of a program.
🌐
Stack Abuse
stackabuse.com › command-line-arguments-in-python
Command Line Arguments in Python
June 19, 2023 - Additionally, the library lets you define the default values, descriptions of, and data type of the arguments, so additional checks and conversions aren't necessary. from absl import flags import sys # Flag name, default value, help message. flags.DEFINE_string('name', 'User', 'The name of the user.') # Read sys.argv into FLAGS FLAGS = flags.FLAGS FLAGS(sys.argv) print(f"Hello {FLAGS.name}!")
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › command-line-arguments-in-python
Command Line Arguments in Python - GeeksforGeeks
Python provides the following built-in modules to handle command-line arguments: sys module: gives access to arguments as a list (sys.argv). getopt module: helps parse arguments similar to how options work in Unix/Linux commands. argparse module: the most powerful and user-friendly way to define, parse, and handle arguments.
Published   December 17, 2025
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-main.html
Python main() - Command Line Arguments
$ python3 affirm.py -affirm Lisa Everything is coming up Lisa $ python3 affirm.py -affirm Bart Looking good Bart $ python3 affirm.py -affirm Maggie Today is the day for Maggie $ Command line arguments, or "args", are extra information typed on the line when a program is run.
🌐
W3Schools
w3schools.com › python › python_arguments.asp
Python Function Arguments
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... Information can be passed into functions as arguments.
🌐
Medium
moez-62905.medium.com › the-ultimate-guide-to-command-line-arguments-in-python-scripts-61c49c90e0b3
The Ultimate Guide to Command Line Arguments in Python Scripts | by Moez Ali | Medium
April 18, 2023 - Compared to other types of user input like prompts and web forms, command-line arguments are more efficient and can be automated easily. They can also be used to run scripts in batch mode, without the need for user interaction. In Python, command-line arguments are accessed through the sys.argv list.
🌐
Codecademy
codecademy.com › article › command-line-arguments-in-python
Command Line Arguments in Python (sys.argv, argparse) | Codecademy
Python Command line arguments are parameters passed to a script when it’s executed from the command line interface. These arguments allow users to customize how a Python program runs without modifying the source code.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-command-line-arguments
Python Command Line Arguments: sys.argv, argparse, getopt | DigitalOcean
3 weeks ago - Learn Python command line arguments with sys.argv, argparse, and getopt. Compare patterns, handle bad input, and copy the examples.
🌐
AskPython
askpython.com › home › python command line arguments – 3 ways to read/parse
Python Command Line Arguments - 3 Ways to Read/Parse - AskPython
December 16, 2019 - Python command line arguments are the parameters provided to the script while executing it. Use sys.argv and argparse module to parse command-line arguments.
🌐
Real Python
realpython.com › command-line-interfaces-python-argparse
Build Command-Line Interfaces With Python's argparse – Real Python
December 14, 2024 - The next step is to call .parse_args() to parse the input arguments and get a Namespace object that contains all the user’s arguments. Note that now the args variable holds a Namespace object, which has a property for each argument that’s been gathered from the command line. In this example, you only have one argument, called path. The Namespace object allows you to access path using the dot notation on args. The rest of your code remains the same as in the first implementation. Now go ahead and run this new script from your command line: ... $ python ls.py sample/ lorem.md realpython.md hello.txt $ python ls.py usage: ls.py [-h] path ls.py: error: the following arguments are required: path $ python ls.py sample/ other_dir/ usage: ls.py [-h] path ls.py: error: unrecognized arguments: other_dir/ $ python ls.py non_existing/ The target directory doesn't exist
🌐
InformIT
informit.com › articles › article.aspx
4.11 Accessing Command-Line Arguments | 22 Python Tricks | InformIT
Alternatively, you can let the program prompt the user for the information needed. But use of command-line arguments is typically more efficient. Command-line arguments are always stored in the form of strings. So—just as with data returned by the input function—you may need to convert this string data to numeric format. To access command-line arguments from within a Python ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › args-kwargs-python
*args and **kwargs in Python - GeeksforGeeks
In Python, *args and **kwargs are used to allow functions to accept an arbitrary number of arguments.
Published   September 20, 2025
🌐
MachineLearningMastery
machinelearningmastery.com › home › blog › command line arguments for your python script
Command Line Arguments for Your Python Script - MachineLearningMastery.com
June 21, 2022 - The part 192.168.0.3:/tmp/ and ./ above are examples. The order of compulsory arguments is important. For example, the rsync command above will copy files from 192.168.0.3:/tmp/ to ./ instead of the other way round. The following replicates the above example in Python using argparse: