There is no standard solution for this option. Usually different libraries are used to deal with command lines arguments from standard lines. https://docs.python.org/3/library/argparse.html before https://click.palletsprojects.com/en/7.x/ like that. But there's another format, like this:python3 code.py arg1 arg2 arg3 --first=1 --two=2 --three=3
A review of the various libraries for the selection of command lines can be read in this article: https://habr.com/ru/post/466999/ Simple stack decision for your format:import sys
import ast
def test(*args, **kwargs):
print(args)
print(kwargs)
print(sys.argv[1:])
args = []
kwargs = {}
for item in sys.argv[1:]:
item = item.rstrip(',')
if item: # Пустой элемент, например если была одиночная запятая
try:
args.append(ast.literal_eval(item))
except (ValueError, SyntaxError):
if "=" not in item:
args.append(item)
else:
key, value = item.split("=", maxsplit=1)
try:
kwargs[key] = ast.literal_eval(value)
except ValueError:
kwargs[key] = value
test(*args, **kwargs)
Conclusion:$ python3 test_args.py 'arg1', 'arg2', 'arg3', first='1', two=2, three='3'
['arg1,', 'arg2,', 'arg3,', 'first=1,', 'two=2,', 'three=3']
('arg1', 'arg2', 'arg3')
{'first': 1, 'two': 2, 'three': 3}
All the rubbers in handing over the command line have been lost, so the numbers in the skirts are just numbers. If you need to hand over the lines, you can simplify the code by removing attempts to convert through help. ast.literal_eval (all parameters will be considered as rows):import sys
def test(*args, **kwargs):
print(args)
print(kwargs)
print(sys.argv[1:])
args = []
kwargs = {}
for item in sys.argv[1:]:
item = item.rstrip(',')
if item: # Пустой элемент, например если была одиночная запятая
if "=" not in item:
args.append(item)
else:
key, value = item.split("=", maxsplit=1)
kwargs[key] = value
test(*args, **kwargs)
The conclusion will be:$ python3 test_args.py 'arg1', 'arg2', 'arg3', first='1', two=2, three='3'
['arg1,', 'arg2,', 'arg3,', 'first=1,', 'two=2,', 'three=3']
('arg1', 'arg2', 'arg3')
{'first': '1', 'two': '2', 'three': '3'}