racket - 如何构建 # :usage-help string dynamically in racket lang's (command-line . ..) 函数?

标签 racket

我正在尝试构建一个 git 风格的 CLI 应用程序,例如 racket-app command command-params ...。这与您可以使用 thor 执行的操作类似。的子命令功能。

我有这个代码:

(define commands
  (hash
   "noop"   (list "Does nothing"
                  (lambda () (log-info "noop was called :)")))
   "render" (list "Prepares and renders the static HTML (hugo)"
                  render-proc)))

(define s1 (build-possible-commands-from-the-keys-of-commands))

(define command
  (command-line
   #:usage-help s1
   #:args (op) op))

(cond
 [(dict-has-key? commands command) ((second (dict-ref commands command)))]
 [else (log-error "Unknown command")])

它失败,命令行:#:usage-help子句包含非字符串

如果我用实际字符串(例如“aoeu”)替换对s1的引用,那么它就可以正常工作。我想动态地构建该字符串(本例中为 s1 ),但我不知道如何做到这一点。

最佳答案

我认为这里的问题是用法帮助必须是一个文字字符串,而不是在程序运行时计算的字符串,因为该字符串应该可以从命令行获得,无需运行程序。也就是说,raco help 应该能够在不运行程序的情况下提供帮助字符串。

你可以解决这个问题;您需要在扩展时提供字符串。这是最直接的方法:

#lang racket

(define-syntax my-cmd
  (syntax-rules ()
    [(_ str) 
     (define command
       (command-line
        #:usage-help str
        #:args (op) op))]))

(my-cmd "aoeu")

在此示例中,很明显我已将“命令”调用与文字字符串分开,但它对您来说可能没那么有用。

所以,问题是:用例是什么?您如何尝试对字符串进行抽象?

编辑

阅读完您的用例后,在我看来,您完全想使用parse-command-line,正如 Jon Zeppieri 在 Racket 邮件列表上建议的那样。这允许您指定当参数解析失败时要调用的过程,并且您可以发出您喜欢的任何字符串。

关于racket - 如何构建 # :usage-help string dynamically in racket lang's (command-line . ..) 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34837318/

相关文章:

testing - 结构的 Rackunit 相等性检查

scheme - Scheme 符号区分大小写

scheme - 为什么(开始)在 Scheme 中有效?

scheme - 压平一次程序

pass-by-reference - 方案 R5RS : pass by reference

performance - ( Racket )哈希集与哈希集的性能!和函数式编程

c - FFMPEG 滤镜图 'warning, too many B-frames in a row'

process - 控制 "current directory"在 racket 中执行子进程

racket - 如何在 DrScheme 中包含文件?

algorithm - 在多项式时间内计算Scheme中两个列表的最长公共(public)子序列