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


Groups > comp.lang.python > #73312

Re: line to argv transformation

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!newsfeed.fsmpi.rwth-aachen.de!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'matches': 0.07; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:t 10': 0.09; 'def': 0.12; 'argument.': 0.16; 'file"': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'command': 0.22; 'header:User-Agent:1': 0.23; 'expanded': 0.24; 'skip:e 30': 0.24; 'header:X-Complaints-To:1': 0.27; 'interface': 0.32; 'something': 0.35; 'but': 0.35; 'list.': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'pardon': 0.84; 'partially': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: line to argv transformation
Date Mon, 16 Jun 2014 13:01:54 +0200
Organization None
References <539EBF61.4050106@rece.vub.ac.be>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bdae98.dip0.t-ipconnect.de
User-Agent KNode/4.11.5
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11083.1402916532.18130.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1402916532 news.xs4all.nl 2958 [2001:888:2000:d::a6]:58576
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:73312

Show key headers only | 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