如何在Common Lisp中移植命令行参数和标志? [英] How to parse command line arguments and flags portably in Common Lisp?

查看:74
本文介绍了如何在Common Lisp中移植命令行参数和标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Common Lisp中获取并解析命令行参数,例如 myscript -a 1 -b 2 将允许我获取值,而 myscript -xyz 也将起作用。我该如何移植(在编译器之间)?

I'd like to get and parse command line arguments in Common Lisp, so that e.g. myscript -a 1 -b 2 will allow me to get the values, and myscript -xyz will work too. How can I do it portably(between compilers)?

推荐答案

您可以尝试 unix-options

You can try unix-options.

(ql:quickload :unix-options)

(defpackage :test
  (:use :cl)
  (:import-from :unix-options
                :&parameters
                :&free
                :with-cli-options))

您可能想使用:use 包,但是如果您想从中导入符号,请不要忘记&免费&参数
该库定义了一个 getopt 函数,该函数类似于传统的 getopt 实用程序。但是它还定义了 with-cli-options ,这有点困难。

You probably want to :use the package, but if you prefer to import symbols from it, don't forget about &free and &parameters. The library defines a getopt function which is similar to the traditional getopt utility. But it also defines with-cli-options, which is a little more lispy.


  • 第一组符号定义选项,即不存在或存在的标志;

  • & parameters 之后的符号,后面必须跟一个值;

  • 不能识别为选项或参数的参数在绑定到& free指定的变量的列表中可用

  • the first set of symbols define options, i.e. flags that are either absent or present;
  • symbols after &parameters define parameters, which must be followed by a value;
  • arguments that are not recognized as either options or parameters are available in a list bound to the variable specified by &free

例如:

(in-package :test)

(defun my-program (&rest cli-args)
  (with-cli-options (cli-args)
      (x y z &parameters a b &free other)
    (list x y z a b other))))

在这里定义程序的入口点。在实际程序中,您可以简单地将第一个列表留空,例如:

Here I define the entry point of the program. In a real program, you can simply leave the first list empty, like this:

(with-cli-options () <bindings> <body>) 

...,这些选项将从实际的选项中方便地获取Lisp实现的命令行参数。您还可以调用(uiop:command-line-arguments)以具有完整的命令行,这似乎支持更多的实现,并且将程序名称作为第一个元素。
上面的功能允许我测试解析器的行为。
请注意,短期权可以分开或合并:

... and the options will be portably fetched from the actual command line arguments of your Lisp implementation. You can also call (uiop:command-line-arguments) to have to full command line, which seems to support more implementations and includes the name of the program as the first element. The above function allows me to test the behavior of the parser. Note for example that short options can be separated or joined:

(my-program "-xyz" "-a" "2" "-b" "3" "--" "something")
=> (T T T "2" "3" ("something"))

(my-program "-x" "-y" "-z" "-a" "2" "-b" "3" "--" "something")
=> (T T T "2" "3" ("something"))

请注意声明的选项作为参数,但没有给出实际值(或者可能是实际情况,模棱两可):

Be careful about options that are declared as parameters but aren't given actual values (or maybe they are, the case is ambiguous):

(my-program "-a" "-b")
=> (NIL NIL NIL "-b" NIL NIL)

对于未知参数有警告:

(ignore-errors (my-program "-w"))
; WARNING: Invalid option: w
=> (NIL NIL NIL NIL NIL NIL)

有关详细信息,请参阅文档。

See the docs for details.

这篇关于如何在Common Lisp中移植命令行参数和标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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