Giter Club home page Giter Club logo

Comments (6)

dnaeon avatar dnaeon commented on August 18, 2024

Hey @simendsjo ,

What is the value associated with the -c|--clip option, when you only invoke these?

cmd foo
cmd -c foo
cmd -c1 foo

I assume there's a default value associated with the -c|--clip option, is that correct? If that's the case, can you simply use a regular int option with a default value?

from clingon.

simendsjo avatar simendsjo commented on August 18, 2024

I assume there's a default value associated with the -c|--clip option, is that correct? If that's the case, can you simply use a regular int option with a default value?

The problem is that I want missing, present and value-set, but an int option can only give me missing (default) and value set, but not "present but without value".
I need to know that the option was missing as that should show all lines to stdout. I don't care what value is stored in clip as long as it's not a positive integer in this case.

clip result
nil
-c 1
-c2 2

from clingon.

dnaeon avatar dnaeon commented on August 18, 2024

Hey @simendsjo ,

Maybe I'm not able to understand the use case, but wouldn't something like this work?

(defun clip/options ()
  (list
   (clingon:make-option :integer
                        :short-name #\c
                        :long-name "clip"
                        :description "the --clip option"
                        :key :clip/option)))

(defun clip/handler (cmd)
  (let ((opt-value (clingon:getopt cmd :clip/option 1))
        (opt-is-set (clingon:opt-is-set-p cmd :clip/option))
        (args (clingon:command-arguments cmd)))
    (format t "--clip option is set: ~A~%" opt-is-set)
    (format t "--clip value is: ~A~%" opt-value)
    (format t "free args: ~A~%" args)))

(defun clip/command ()
  (clingon:make-command
   :name "clip"
   :description "clip command"
   :handler #'clip/handler
   :options (clip/options)))

Option is not specified on command-line, and it's value is 1.

$ cmd foo
--clip option is set: NIL
--clip value is: 1
free args: (foo)

Option is specified on the command-line.

$ cmd -c 42 foo
--clip option is set: T
--clip value is: 42
free args: (foo)

Supporting a command-line like this in my opinion is very ambiguous, where -c option might receive a value, or not.

$ cmd -c foo

In this case it's not clear whether -c is a flag (option with no arguments), or whether foo is the value associated with -c option.

from clingon.

simendsjo avatar simendsjo commented on August 18, 2024

Maybe I'm not able to understand the use case, but wouldn't something like this work?

No, unfortunately, because cmd -c foo will consume foo even though it's not an integer.

Supporting a command-line like this in my opinion is very ambiguous, where -c option might receive a value, or not.

Yes, I totally agree, but I'm trying to be backwards compatible with an existing API so tools also works with my implementation.

I guess I can solve this by fetching uiop:command-line-arguments myself, look for -c not-an-integer and change it to -c1 not-an-integer. Quite hackish, but at least it should work.

Yes, not a very good API design :/

from clingon.

simendsjo avatar simendsjo commented on August 18, 2024

Here's my solution. Replaces "-c" with "-c1" and "--clip" with "--clip=1" if it's not followed by an integer.
And I pass the result to clingon:run. Good enough for me :)

(cl:defun patch-args-for-clip (args cl:&aux (result (cl:copy-list args)))
  (cl:dolist (spec (cl:list (cl:cons "-c" "-c1")
                            (cl:cons "--clip" "--clip=1"))
                   result)
    (cl:let* ((old (cl:car spec))
              (new (cl:cdr spec))
              (opt (cl:member old result :test 'cl:equal))
              (val (cl:cadr opt))
              (int? (cl:when val (cl:parse-integer val :radix 10 :junk-allowed cl:t))))
      (cl:when (cl:and opt (cl:not int?))
        (cl:nsubst new old result :test 'cl:equal)))))

from clingon.

simendsjo avatar simendsjo commented on August 18, 2024

Thanks for all your support! Guess we can close this as a won't fix as it's not really a recommended API design.

from clingon.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.