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
Here Python script name is script.py and rest of the three arguments - arg1 arg2 arg3 are command line arguments for the program. If the program needs to accept input from the user, Python's input() function is used.
Discussions

Modifying Python Script to Accept Command Line Arguments?
https://docs.python.org/3/library/argparse.html More on reddit.com
🌐 r/learnpython
5
2
March 3, 2024
Is it possible to pass arguments into a Python script? - Unix & Linux Stack Exchange
I know how to pass arguments into a shell script. These arguments are declared in AWS datapipeline and passed through. This is what a shell script would look like: firstarg=$1 secondarg=$2 How do ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
October 20, 2015
command line interface - Get user input with arguments in Python - Stack Overflow
TL;DR I need to get a user input that contains an argument in order to do something, I need my own script that gets user input, and acts like it's its own interpreter. My goal is to make my own CLI with my own commands. What I need now is to get user input within a python script. More on stackoverflow.com
🌐 stackoverflow.com
Passing command line arguments to python scripts
Hi, I've just started using VS Code for python scripting. It looks rather complete so far. One thing I can't yet figure out is how to add command… More on reddit.com
🌐 r/vscode
6
5
October 9, 2017
🌐
GeeksforGeeks
geeksforgeeks.org › python › command-line-arguments-in-python
Command Line Arguments in Python - GeeksforGeeks
In Python, command line arguments are values passed to a script when running it from the terminal or command prompt. They act like inputs, allowing you to change a program’s behavior without modifying the code.
Published   December 17, 2025
🌐
Medium
medium.com › @evaGachirwa › running-python-script-with-arguments-in-the-command-line-93dfa5f10eff
Running Python script with Arguments in the command line | by Eva Mwangi | Medium
April 22, 2023 - The sys module provides functions and variables to manipulate Python’s runtime environment. Through sys.argv arguments are passed from the command line to the Python script.
🌐
Python
docs.python.org › 3 › library › argparse.html
argparse — Parser for command-line options, arguments and subcommands
Source code: Lib/argparse.py Tutorial: This page contains the API reference information. For a more gentle introduction to Python command-line parsing, have a look at the argparse tutorial. The arg...
🌐
Reddit
reddit.com › r/learnpython › modifying python script to accept command line arguments?
r/learnpython on Reddit: Modifying Python Script to Accept Command Line Arguments?
March 3, 2024 -

I am trying to modify an already existing code (https://github.com/ohyicong/decrypt-chrome-passwords/blob/main/decrypt_chrome_password.py) to take in command line arguments an example: python script.py "path_to_Local_State_file" "path_to_Login_Data_file" I think that this should be a pretty simple fix but chatGPT is not giving me the correct code when I test it how should I approach this.

Find elsewhere
🌐
Real Python
realpython.com › python-command-line-arguments
Python Command-Line Arguments – Real Python
August 27, 2023 - $ python zen_sort_stderr.py | sort DEBUG >>> About to print the Zen of Python DEBUG >>> Done printing the Zen of Python Although never is often better than *right* now. Although practicality beats purity. Although that way may not be obvious at first unless you're Dutch .... Now, the traces are displayed to the terminal, but they aren’t used as input for the sort command. You can implement seq by relying on a regular expression if the arguments aren’t too complex. Nevertheless, the regex pattern may quickly render the maintenance of the script difficult.
🌐
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.
🌐
Analytics Vidhya
analyticsvidhya.com › home › 3 easy ways to handle command line arguments in python
3 Easy Ways to Handle Command Line Arguments in Python
January 15, 2025 - Command line arguments are a powerful tool for making your Python scripts more flexible and reusable. They allow you to pass args to python script different inputs to your script each time you run it, making it more versatile. We’ve explored three easy ways to handle command line arguments – using sys.argv, the getopt module, and the argparse module.
🌐
LabEx
labex.io › tutorials › python-how-to-execute-scripts-with-parameters-418940
How to execute scripts with parameters | LabEx
The guide will cover fundamental ... Python scripting capabilities. Script arguments are input values passed to a Python script when it is executed from the command line....
🌐
Codecademy
codecademy.com › article › command-line-arguments-in-python
Command Line Arguments in Python (sys.argv, argparse) | Codecademy
Depending on your needs, from simple scripts to complex applications, Python offers several ways to handle these arguments efficiently. In this article, we explored three main methods for working with command-line arguments in Python: sys.argv, which is straightforward and suited for positional arguments. getopt, which introduces support for short and long options with basic error handling. argparse, the most feature-rich option that provides input validation, automatic help messages, and flexible argument types.
🌐
MachineLearningMastery
machinelearningmastery.com › home › blog › command line arguments for your python script
Command Line Arguments for Your Python Script - MachineLearningMastery.com
June 21, 2022 - We can further require an argument to be a specific type. For example, in the -B option above, we can make it expect integer data by adding type like the following: And if we provided the wrong type, argparse will help terminate our program with an informative error message: Empowering your Python script with command line arguments can bring it to a new level of reusability.
🌐
Hyperskill
hyperskill.org › university › python › command-line-arguments-in-python
Command Line Arguments in Python
July 30, 2024 - In Python command line arguments are the inputs given to a script while it runs, enabling the script to manage data sets without changing the code.
🌐
OpenSourceOptions
opensourceoptions.com › how-to-pass-arguments-to-a-python-script-from-the-command-line
How to Pass Arguments to a Python Script from the Command Line – OpenSourceOptions
The script will have the possibility of four named arguments, ‘help’, ‘input’, ‘user’, and ‘output’. From the command line, these arguments can be specified with a single dash and the first letter (-h) or a double dash and the full argument name (--help).
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-command-line-arguments
Python Command Line Arguments: sys.argv, argparse, getopt | DigitalOcean
March 25, 2026 - 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. The command-line arguments are used to provide specific inputs to the program.
🌐
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 - 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.
🌐
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
This will inspect the command line, convert each argument to the appropriate type and then invoke the appropriate action. In most cases, this means a simple Namespace object will be built up from attributes parsed out of the command line: >>> parser.parse_args(['--sum', '7', '-1', '42']) Namespace(accumulate=<built-in function sum>, integers=[7, -1, 42]) In a script, parse_args() will typically be called with no arguments, and the ArgumentParser will automatically determine the command-line arguments from sys.argv.
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("")