Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73316 > unrolled thread

Re: line to argv transformation

Started byAntoon Pardon <antoon.pardon@rece.vub.ac.be>
First post2014-06-16 13:51 +0200
Last post2014-06-16 13:51 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: line to argv transformation Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-06-16 13:51 +0200

#73316 — Re: line to argv transformation

FromAntoon Pardon <antoon.pardon@rece.vub.ac.be>
Date2014-06-16 13:51 +0200
SubjectRe: line to argv transformation
Message-ID<mailman.11087.1402919466.18130.python-list@python.org>
On 16-06-14 13:01, Peter Otten wrote:
> Antoon Pardon wrote:
>
>> 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.
> You might combine shlex and glob:
>
> def parse_and_expand(s):
>     parts = shlex.split(s)
>     expanded = []
>     for part in parts:
>         matches = glob.glob(part)
>         if matches:
>             expanded.extend(sorted(matches))
>         else:
>             expanded.append(part)
>     return expanded
No that doesn't work because of this:

>>> shlex.split("ls *.py")
['ls', '*.py']
>>> shlex.split("ls '*.py'")
['ls', '*.py']
>>>

After the split you have no idea wether the glob pattern you see was quoted
or not.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web