Python Forum
Taking Multiple Command Line Argument Input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Taking Multiple Command Line Argument Input
#1
Hi,guys! Can you help me with how to take any number of command-line inputs from user without defining any limit? I have tried to generated a few partial solutions such as using split() function or list compherension but non of them worked quite good.
Reply
#2
Can you give examples of what you mean please (both input/output and code)? sys.argv already gives you a list of the arguments, so I don't know what you're splitting.
Reply
#3
(Mar-29-2020, 03:32 PM)ndc85430 Wrote: Can you give examples of what you mean please (both input/output and code)? sys.argv already gives you a list of the arguments, so I don't know what you're splitting.

I have to write a command line argument so the user can write any number of inputs, number can change in every iteration up to user. I mean user can write 1 2 3 4 to the computer in first time she/he running the program but also can write 60 4 5 6 1 9 7 4
in another time.

a, b = input("Enter a two value: ").split()
print("First number is {} and second number is {}".format(a, b))
print()
This is the split example but there is a problem in here which is, the number of inputs are limited by the coder with 2. Which I don't want.

x, y, z = [int(x) for x in input("Enter three value: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print("Third Number is: ", z)
print()

This is an example of the other method an has the same problem, here the number of inputs are limited with 3.
Reply
#4
Ok, so what you're asking about isn't really "command line arguments" - those are values passed to the program when it's started (e.g. consider python foo.py a b c - the values a, b and c are command line arguments).

If you're insisting on unpacking the values like that, then no, there isn't a way to do that. Why do you think you need to unpack them, rather than just doing the split and say, iterating over the list with a for loop?
Reply
#5
Try this
import sys

argument_list = sys.argv
how_many = len(argument_list)

print(f'You entered {how_many} arguments')
print('They are')
for passed in argument_list :
    print(f'{passed}')
Output:
C:\Users\Jeff\foo>python arguments.py 45 28 16 1000 2 hello world You entered 8 arguments They are arguments.py 45 28 16 1000 2 hello world
Reply
#6
(Mar-29-2020, 05:16 PM)jefsummers Wrote: Try this
import sys

argument_list = sys.argv
how_many = len(argument_list)

print(f'You entered {how_many} arguments')
print('They are')
for passed in argument_list :
    print(f'{passed}')
Output:
C:\Users\Jeff\foo>python arguments.py 45 28 16 1000 2 hello world You entered 8 arguments They are arguments.py 45 Thank you very much! You are the best it helped a lot. 28 16 1000 2 hello world
Thank you very much! You are the best it helped a lot.
Reply
#7
(Mar-29-2020, 05:42 PM)bwdu Wrote: Thank you very much! You are the best it helped a lot.
note that sys.argv is the most basic way to work with command line arguments.
There is argparse module in the standard library.
there are plenty of packages that make it easy to create nice CLI interfaces, e.g. Click
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  calling a function and argument in an input phillup7 3 200 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  How to print string multiple times on new line ace19887 7 274 Sep-30-2020, 02:53 PM
Last Post: buran
  Passing List of Objects in Command Line Python usman 7 458 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  EOF error while taking input ShishirModi 1 268 Sep-27-2020, 11:28 AM
Last Post: jefsummers
  bad input line 3 how t fix it youssef210 2 303 Aug-27-2020, 04:57 PM
Last Post: nilamo
  Line charts error "'isnan' not supported for the input types," issac_n 1 353 Jul-22-2020, 04:34 PM
Last Post: issac_n
  How to print out multiple drinks instead of just one based on the input? jayfre 4 713 Jul-01-2020, 06:53 PM
Last Post: menator01
  taking input doesnt print as list bntayfur 2 357 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  SyntaxError: positional argument follows keyword argument syd_jat 3 2,025 Mar-03-2020, 08:34 AM
Last Post: buran
  python 3 from command line Dixon 1 428 Mar-01-2020, 08:35 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020