0

So, what i am trying to do is LS Mimic in Python code.

I have completed most of the commands, but i have one thing that i cant find the solution

So, if the ls command gets this command line,

ls test1

It should find the test1 directory and then do the ls in that directory.

However, since I can only find ways of creating arguments that needs a keyword before the actual usage, I cant find the way of doing this..

it cant be something like this

ls -move_dir test1

it is fine if for the program to think if there is no command, it will treat as above way. ( it will find that directory and run ls )

Please help me !!

3
  • 4
    argparse supports positional arguments. Commented Nov 5, 2012 at 18:25
  • Oh yea but i forgot to mention that positional argument is when you have to have that argument. but in the above case, that is not correct. anyways thanks! Commented Nov 6, 2012 at 8:27
  • The answer you've accepted is using a positional argument. I supposed I could have mentioned that you needed to look into using the nargs parameter in addition to using a positional argument... :) Commented Nov 6, 2012 at 8:36

2 Answers 2

4

Does this help?

import argparse
p = argparse.ArgumentParser()
p.add_argument('a', nargs='*')
res = p.parse_args()
print res.a

Result:

% python test.py 1 2 3
['1', '2', '3']
Sign up to request clarification or add additional context in comments.

Comments

-2

try looking into using sys.argv[]

1 Comment

Ugh.. it could work but i already have implemented everything in argparse and i like the way argparse works.. but thanks for the reply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.