Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'received:134': 0.05; 'matches': 0.07; 'string': 0.09; 'subject:skip:t 10': 0.09; 'def': 0.12; 'argument.': 0.16; 'file"': 0.16; 'quoted': 0.16; 'wether': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'split': 0.19; 'command': 0.22; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'expanded': 0.24; 'skip:e 30': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; "doesn't": 0.30; 'interface': 0.32; 'not.': 0.33; '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; 'otten': 0.84; 'pardon': 0.84; 'partially': 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqIEAALZnlOGuA9G/2dsb2JhbABahyW9G4MSAYEnhHgBAQQBIwRREQsYAgIFFgsCAgkDAgECAUUTCAKINgixFZhmhSYXgSqNUxaCYYFMAQOaQ4ZjjHWDQg Date: Mon, 16 Jun 2014 13:51:04 +0200 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Icedove/24.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: line to argv transformation References: <539EBF61.4050106@rece.vub.ac.be> In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1402919466 news.xs4all.nl 2893 [2001:888:2000:d::a6]:43432 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73316 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.