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


Groups > comp.lang.python > #73312

Re: line to argv transformation

From Peter Otten <__peter__@web.de>
Subject Re: line to argv transformation
Date 2014-06-16 13:01 +0200
Organization None
References <539EBF61.4050106@rece.vub.ac.be>
Newsgroups comp.lang.python
Message-ID <mailman.11083.1402916532.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: line to argv transformation Peter Otten <__peter__@web.de> - 2014-06-16 13:01 +0200

csiph-web