访问原始命令行? [英] Access to raw command line?

查看:72
本文介绍了访问原始命令行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在用Python编写一个命令行程序,它采用以下形式的

命令:

../myprog.py [OPTIONS] ARGS

非常标准的东西。就我而言,ARGS是一个图像文件列表。


其中一个可能的选项是指定一个文件,其中包含有关照片的信息

。您将(在此特定情况下)指定它 -

t开关,并且您可以通过重复此开关指定多个文件:

../myprog.py -t info1.gpx -t info2.gpx -t info3.gpx * jpg


现在,其中一个用户拥有相当多的信息文件,并问我是否

它可以使用通配符来指定这些,所以他只需要

必须这样做:

../myprog.py -t * .gpx * .jpg


这看起来似乎是一个明智的选择,但实施起来很难实现,因为通配符是通过扩展的shell,所以

sys.argv获取一个包含" -t",所有.gpx文件和所有.jpg

文件的列表。有了这个列表,就没有办法告诉哪些文件属于

" -t"切换和哪些是参数(除了使用

扩展名)。


解决此问题的一种可能方法是获取原始命令行

并在Python中自己进行shell扩展。忽略

是否值得麻烦的问题,是否有人知道它是否可以获得原始(未扩展)命令行?

或者,是否有人建议如何以

清洁方式进行此操作?


谢谢,

Pieter

Hi,

I''m currently writing a command-line program in Python, which takes
commands in the form of:
../myprog.py [OPTIONS] ARGS
So pretty standard stuff. In my case, ARGS is a list of image files.

One of the possible options is to specify a file holding information
about the photos. You''d specify it with (in this particular case) the -
t switch, and you can specify multiple files by repeating this switch:
../myprog.py -t info1.gpx -t info2.gpx -t info3.gpx *jpg

Now, one of the users has quite a lot of info files, and asked me if
it''s possible to use a wildcard in specifying these, so he would just
have to do:
../myprog.py -t *.gpx *.jpg

This seems like a sensible option at first sight, but it''s difficult
to implement because the wildcard is expanded by the shell, so
sys.argv gets a list containing "-t", all .gpx files and all .jpg
files. With this list, there''s no way to tell which files belong to
the "-t" switch and which are arguments (other than using the
extension).

One possible way to work around this is to get the raw command line
and do the shell expansions ourselves from within Python. Ignoring the
question of whether it is worth the trouble, does anybody know if it
is possible to obtain the raw (unexpanded) command line?
Alternatively, does anybody have suggestion of how to do this in a
clean way?

Thanks,
Pieter

推荐答案

Pieter Edelmanaécrit:

< zip>
Pieter Edelman a écrit :
<zip>

解决这个问题的一种可能方法是获取原始命令行

并自己在Python中进行shell扩展。忽略

是否值得麻烦的问题,是否有人知道它是否可以获得原始(未扩展)命令行?

或者,是否有人建议如何以一种干净的方式执行此操作?
One possible way to work around this is to get the raw command line
and do the shell expansions ourselves from within Python. Ignoring the
question of whether it is worth the trouble, does anybody know if it
is possible to obtain the raw (unexpanded) command line?
Alternatively, does anybody have suggestion of how to do this in a
clean way?



为了避免shell扩展,请尝试引用args:

../myprog.py -t" * .gpx" " *。jpg"

或(恕我直言)简单定义anoter开关以识别其他文件:

../myprog.py -t * .gpx -u *。 jpg


A +


Laurent。

To avoid shell extension, try to quote args:
../myprog.py -t "*.gpx" "*.jpg"
Or (IMHO simpler) define anoter switch to identify other files:
../myprog.py -t *.gpx -u *.jpg

A+

Laurent.


En Thu,2007年4月26日04 :04:30 -0300,Pieter Edelman< p。******* @ gmail.com>
$ b $bescribió:
En Thu, 26 Apr 2007 04:04:30 -0300, Pieter Edelman <p.*******@gmail.com>
escribió:

现在,其中一个用户拥有相当多的信息文件,并问我是否可以使用通配符来指定这些,所以他只需要

必须这样做:

./myprog.py-t * .gpx * .jpg


这看起来似乎是一个明智的选择,但是很难实现
,因为通配符是由shell扩展的,所以

sys.argv获取一个包含-t的列表,所有.gpx文件和所有.jpg

文件。有了这个列表,就没有办法告诉哪些文件属于

" -t"切换和哪些是参数(除了使用

扩展名)。
Now, one of the users has quite a lot of info files, and asked me if
it''s possible to use a wildcard in specifying these, so he would just
have to do:
./myprog.py -t *.gpx *.jpg

This seems like a sensible option at first sight, but it''s difficult
to implement because the wildcard is expanded by the shell, so
sys.argv gets a list containing "-t", all .gpx files and all .jpg
files. With this list, there''s no way to tell which files belong to
the "-t" switch and which are arguments (other than using the
extension).



我现在无法验证,但我认为使用-t" * .gpx"将避免外壳扩展的b / b
。 (或者也许''* .gpx''或\ * .gpx或甚至-t * .gpx)


-

Gabriel Genellina

I can''t verify right now, but I think that using -t "*.gpx" would avoid
the shell expansion. (Or perhaps ''*.gpx'' or \*.gpx or even -t*.gpx)

--
Gabriel Genellina


Pieter Edelman写道:
Pieter Edelman wrote:

./myprog.py -t * .gpx * .jpg


这看起来似乎是一个明智的选择,但它很难实现,因为通配符是由

shell扩展的,
./myprog.py -t *.gpx *.jpg

This seems like a sensible option at first sight, but it''s
difficult to implement because the wildcard is expanded by the
shell,



(仅限* nix)

(in *nix only)


所以sys.argv获取一个包含 - 的列表 - t",所有.gpx文件和

所有.jpg文件。有了这个列表,就没有办法告诉哪些文件

属于-t。切换和哪些是参数(除了

使用扩展名)。


解决这个问题的一种可能方法是获取原始命令

行,并在Python中自己进行shell扩展。

忽略值得麻烦的问题,确实

任何人都知道是否可以获取原始(未扩展)

命令行?
so sys.argv gets a list containing "-t", all .gpx files and
all .jpg files. With this list, there''s no way to tell which files
belong to the "-t" switch and which are arguments (other than
using the extension).

One possible way to work around this is to get the raw command
line and do the shell expansions ourselves from within Python.
Ignoring the question of whether it is worth the trouble, does
anybody know if it is possible to obtain the raw (unexpanded)
command line?



打破事实上的标准不是一个好主意,恕我直言。

Breaking the "de-facto standards" isn''t a good idea, IMHO.


或者,是否有人建议如何以一种干净的方式在

中这样做?
Alternatively, does anybody have suggestion of how to do this in
a clean way?



首先,你可以定义另一个通配符,或者让用户

逃避*如下:./ myprog.py -t \\ \\ * .gpx \ * .jpg


让用户提供要处理的文件列表,默认情况下查看

的相同文件名没有图片扩展名和.gpx。

E. g。:


../myprog.py-t .gpx * .jpg


将扩展为:


../myprog.py -t .gpx a.jpg b.jpg [...] z.jpg


您的计划的政策是什么?将取决于它的确切功能。

我正在考虑如果程序没有找到

a .gpx文件中的一个.jpg,是否会默默地失败,例如,或者如果它抱怨它。


问候,

Bj?rn


-

BOFH借口#380:

当大量备份磁带倒下时,运营商遇难。

First, you could define another wildcard character, or let users
escape the * like this: ./myprog.py -t \*.gpx \*.jpg

Let users give a list of files to process, and default to looking
for the same filename without image extension and with ".gpx".
E. g.:

../myprog.py -t .gpx *.jpg

will expand to:

../myprog.py -t .gpx a.jpg b.jpg [...] z.jpg

What your program''s "policy" would be depends on its exact function.
I''m thinking about if the program silently fails if it doesn''t find
a .gpx file for one .jpg, for example, or if it complains about it.

Regards,
Bj?rn

--
BOFH excuse #380:

Operators killed when huge stack of backup tapes fell over.

这篇关于访问原始命令行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆