top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Line to argv transformation in Python?

+2 votes
227 views

I am looking for an interface that takes a string as argument. The string is to be treated as if it is a command line and transformed into an argv list.

"ls file" -> ['ls', 'file']
"ls *.py" -> ['ls', 'file1.py', 'file2.py', ...]
"ls '*.py'" -> ['ls', '*.py']

Does something like this already exist? I looked around but seem to find only things only partially do things like this, like shlex.split.

posted Jun 16, 2014 by Sridharan

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
0 votes

I am writing a command line tool in python to generate one time passwords/tokens. The command line tool will have certain sub-commands like --generate-token and --list-all-tokens for example. I want to restrict access to certain sub-commands. In this case, when user tries to generate a new token, I want him/her to authenticate against AD server first.

I have looked at python-ldap and I am even able to bind to the AD server. In my application I have a function

 def authenticate_user(username, password): pass

which gets username and plain-text password. How do I use the LDAPObject instance to validate these credentials?

+1 vote

The source code:

for i in range(8):
 n = input()

When we run it, consider the numbers below is the user input,

1
2
3
(and so forth)

my question, can i make it in just a single line like,

1 2 3 (and so forth)
+2 votes

I have a multi-line string and I need to remove the very first line from it. How can I do that? I looked at StringIO but I can't seem to figure out how to properly use it to remove the first line.

+1 vote

I'm using Python 2.7 under Windows and am trying to run a command line program and process the programs output as it is running. A number of web searches have indicated that the following code would work.

import subprocess

p = subprocess.Popen("D:PythonPython27Scriptspip.exe list -o",
 stdout=subprocess.PIPE,
 stderr=subprocess.STDOUT,
 bufsize=1,
 universal_newlines=True,
 shell=False)
for line in p.stdout:
 print line

When I use this code I can see that the Popen works, any code between the Popen and the for will run straight away, but as soon as it gets to the for and tries to read p.stdout the code blocks until the command
line program completes, then all of the lines are returned. Does anyone know how to get the results of the program without it blocking?

0 votes

I am writing a program that gets its parameters from a combination of config file (using configparser) and command line arguments (using argparse). Now I would also like the program to be able to _write_ a
configparser config file that contains only the parameters actually given on the commandline. Is there a simple way to determine which command line arguments were actually given on the commandline, i.e. does argparse.ArgumentParser() know which of its namespace members were actually hit during parse_args().

I have tried giving the arguments default values and then looking for those having a non-default value but this is really awkward, especially if it comes to non-string arguments. Also, parsing sys.argv looks clumsy because you have to keep track of short and long options with and without argument etc. i.e. all things that I got argparse for in the first place.

...