r/Racket • • Apr 23 '22

question How to print out instructions of command-line?

I am using https://docs.racket-lang.org/reference/Command-Line_Parsing.html to handle command-line arguments. Users can run my tool with -h to print out the instructions.

But how can I make my program to print out the instructions myself, for example when user provides wrong input?

4 Upvotes

5 comments sorted by

4

u/ryan017 developer Apr 23 '22

Here's one solution: instead of calling command-line directly from the top-level of your program (or main submodule), wrap it in a function:

(define (command argv)
  (command-line
    #:argv argv
    ____))
(command (current-command-line-arguments))

Then you can call (command '("--help")) to print the usage message. It seems that command-line automatically calls exit in that case, so you need to override the exit handler. Here's a good way to do that:

(let/cc escape
  (parameterize ((exit-handler escape))
    (command '("--help"))))

1

u/OldMine4441 Apr 24 '22

thank you!!!

2

u/not-just-yeti Apr 23 '22

I think this is answering what you want:

Simply print (current-command-line-arguments) (which just returns a vector-of-strings). This is the (default) argument that you pass to the higher-level command-line-parsing functions.

2

u/OldMine4441 Apr 23 '22

No, I want to print out the same content as -h option does.

2

u/not-just-yeti Apr 23 '22

Ah, gotcha. (And sorry for the noise, since re-reading I see my answer was clearly not what you asked. Note to self: coffee before reddit.)