[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: guile-config: Command --help is not displayed when there are require
From: |
Alex Sassmannshausen |
Subject: |
Re: guile-config: Command --help is not displayed when there are required arguments |
Date: |
Wed, 19 Feb 2020 14:31:56 +0100 |
User-agent: |
mu4e 1.2.0; emacs 26.3 |
Hi Sirgazil,
sirgazil <address@hidden> writes:
> Hi,
>
> When using "guile-config" for declaring my CLIs I've noticed that if I
> define an argument as non-optional for a command, then trying to
> display the --help of that command fails.
You are correct. This issue arises because guile-config uses (ice-9
getopt-long) internally to parse the command line arguments and handle
all the usual gnu cli argument niceties.
However this causes a chicken and egg problem: to execute help when the
--help flag is provide we need to have parsed the cli options. But
getopt-long throws an error whilst parsing the cli options as soon as it
detects a mandatory argument is missing.
This is not ideal. The solution would probably be have to be to
re-implement cli argument parsing outside of getopt-long, or to patch
getopt-long to not throw on validation-error but to return an
error-structure, which includes the parsed cli args, but also any
problems it encountered during validation.
Dunno which is better at this point, but I'll log your report and my
thoughts as a bug against guile-config for now.
Of course, patches implementing either solution, welcome! :-)
Best wishes,
Alex
>
> Consider the following script which displays "What yummy apple!" when it's
> run like "$ eat apple":
>
>
> #+BEGIN_SRC scheme
> (use-modules (config)
> (config api)
> (ice-9 format)
> (ice-9 match))
>
>
> ;;; CONSTANTS
> ;;; =========
>
> (define FRUITS
> (list "apple" "banana" "kiwi" "mango"))
>
>
>
> ;;; VALIDATORS
> ;;; ==========
>
> (define (fruit? fruit)
> (member fruit FRUITS))
>
>
>
> ;;; ARGUMENTS AND OPTIONS
> ;;; =====================
>
> (define fruit
> (argument (name 'fruit)
> (synopsis "The name of a fruit.")
> (example "apple")
> (optional? #false)
> (test fruit?)))
>
>
>
> ;;; CLI SPECIFICATION
> ;;; =================
>
> (define spec
> (configuration
> (name 'eat)
> (synopsis "Eat imaginary fruits.")
> (version "1.0")
> (arguments (list fruit))))
>
>
>
> ;;; CLI DISPATCHER
> ;;; ==============
>
> (define (dispatcher command spec)
> #| Execute the appropriate procedure for the given COMMAND if it is
> part of the given command-line interface specification.
>
> COMMAND (list of strings)
> A list of strings that represent a command-line instruction passed
> to the program (see the Guile command-line procedure).
>
> SPEC (Configuration)
> A <configuration> record as specified by the guile-config library
> specifying the command-line interface of the program.
>
> RESULT
> Execute the COMMAND or display help information if the COMMAND is
> not recognized. |#
> (let* ((options (getopt-config-auto command spec))
> (fruit (option-ref options '(fruit))))
> (catch 'match-error
> (lambda ()
> (match (full-command options)
> ((_) (format #true "What yummy ~a!!~%" fruit))))
>
> (lambda (. args)
> (begin (display "ERROR: Command not found.")
> (newline) (newline)
> (emit-help options))))))
>
>
>
> ;;; RUN
> ;;; ===
>
> (dispatcher (command-line) spec)
>
> #+END_SRC
>
>
> Now, when I try to display the help information of the "eat" command, I get
> this message:
>
>
> #+BEGIN_EXAMPLE
> $ eat --help
> error: eat: argument must be specified: fruit
> Usage: eat
>
> Options:
> #+END_EXAMPLE
>
>
> Is this a bug or am I doing something wrong?
>
>
> ---
> https://sirgazil.bitbucket.io/