# How to use mandatory arguments on the command line

**URL:** <https://racket.discourse.group/t/how-to-use-mandatory-arguments-on-the-command-line/655>\
**Category:** Questions & Answers\
**Tags:** question\
**Created:** [February 4, 2022, 11:11pm UTC](https://racket.discourse.group/t/how-to-use-mandatory-arguments-on-the-command-line/655 "2022-02-04T23:11:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dstorrs](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dstorrs](https://racket.discourse.group/u/dstorrs)\
**Post date:** [February 4, 2022, 11:11pm UTC](https://racket.discourse.group/t/how-to-use-mandatory-arguments-on-the-command-line/655/1 "2022-02-04T23:11:03Z")

</div>

I'm refreshing on [15.9&nbsp;Command-Line Parsing](https://docs.racket-lang.org/reference/Command-Line_Parsing.html#%28form._%28%28lib._racket%2Fcmdline..rkt%29._command-line%29%29) and I note that there doesn't seem to be a way to say "this flag must be present." Am I missing something?

Here's the normal workaround:

```scheme
#lang racket

(define name (make-parameter #f))
(define height (make-parameter #f))
(define phone-numbers (make-parameter '()))

(command-line
 #:program "describe person"
 #:once-each
 [("-n" "--name") nm "Specify person's name (mandatory)" (name nm)]
 [("-H" "--height") h "Specify height in cm" (height h)]
 #:multi
 [("-p" "--phone") p "Add another phone number" (phone-numbers (cons p (phone-numbers)))])

(when (not (name))
  (displayln "You must specify the name")
  (exit 0))

(displayln (~a "name: " (name)))
(displayln (~a "height in cm: " (height)))
(displayln (~a "phone num(s): " (phone-numbers)))

```

I could of course use a non-flagged argument after the flags, but I disprefer that option in cases where there are multiple mandatory items, for the same reason that we use keyword arguments on functions: remembering argument order can be a drag.

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [February 5, 2022, 1:38am UTC](https://racket.discourse.group/t/how-to-use-mandatory-arguments-on-the-command-line/655/2 "2022-02-05T01:38:36Z")

</div>

I agree that I don't see an obvious way to accomplish that with the current set of specifiers. Maybe a pull request to add a specifier to the command-line implementation would make sense?

---

<div class="post-metadata">

**Author:** ![dstorrs](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dstorrs](https://racket.discourse.group/u/dstorrs)\
**Post date:** [February 5, 2022, 5:19am UTC](https://racket.discourse.group/t/how-to-use-mandatory-arguments-on-the-command-line/655/3 "2022-02-05T05:19:27Z")

</div>

I'm willing, but I'll need some information on how.

I went to the reference page: [15.9&nbsp;Command-Line Parsing](https://docs.racket-lang.org/reference/Command-Line_Parsing.html)

I clicked on the title and got this:

```scheme
Link to this section with
 @secref["Command-Line_Parsing"
         #:doc '(lib "scribblings/reference/reference.scrbl")]
Document source
 https://github.com/racket/racket/tree/master/pkgs/racket-doc/scribblings/reference/reference.scrbl

```

I clicked the 'document source' link and got to the github repository on a page that has nothing whatsoever to do with command line parsing, which makes me think this is not the right place to make suggestions to the documentation.

If I want to locate the actual documentation, or even the implementation, do I have a better option than simply `grep -r`?

---

<div class="post-metadata">

**Author:** ![simonls](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/simonls/32/170_2.png) [@simonls](https://racket.discourse.group/u/simonls)\
**Post date:** [February 5, 2022, 6:02am UTC](https://racket.discourse.group/t/how-to-use-mandatory-arguments-on-the-command-line/655/4 "2022-02-05T06:02:59Z")

</div>

I agree that in this particular case these links aren't very helpful for someone who wants to contribute, it would be nice if this feature could be improved, but I am unsure whether that would be easy or difficult.

One trick I use in situations like this is:

- I look for an identifier that looks fairly rare / unique
- use that identifier with the github search

Here is an example, I used `#:help-labels` and searched within the `racket/racket` repository (depending on what code it is you may have to do a github wide search, in that case the identifier should be very unique so that this works): [https://github.com/racket/racket/search?q=%23%3Ahelp-labels](https://github.com/racket/racket/search?q=%23%3Ahelp-labels)

* * *

Another approach is to use drracket's `open Defining File` but that is only helpful if you want to edit code, less helpful to find the actual scribble file that contains the documentation.

* * *

Then last resort for me is sort of like using `grep -r`, but instead I use `rg` [https://github.com/BurntSushi/ripgrep](https://github.com/BurntSushi/ripgrep) which is way faster then grep and e.g. automatically ignores files that are ignored by gitignores etc.  
[https://github.com/BurntSushi/ripgrep#why-should-i-use-ripgrep](https://github.com/BurntSushi/ripgrep#why-should-i-use-ripgrep)

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [February 5, 2022, 6:19pm UTC](https://racket.discourse.group/t/how-to-use-mandatory-arguments-on-the-command-line/655/5 "2022-02-05T18:19:54Z")

</div>

Finding the implementation is pretty easy: just hover over a use of command-line, right click, and use "Open Defining File". This should open the file containing the implementation of command-line. N.B. this assumes you're using DrRacket.

---

<div class="post-metadata">

**Author:** ![simonls](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/simonls/32/170_2.png) [@simonls](https://racket.discourse.group/u/simonls)\
**Post date:** [February 6, 2022, 5:45am UTC](https://racket.discourse.group/t/how-to-use-mandatory-arguments-on-the-command-line/655/6 "2022-02-06T05:45:56Z")

</div>

How difficult would it be to implement a "Open Documenting File"?  
I really haven't looked into how all that scribble stuff works at all.  
What would be needed?

1. find out whether a binding has documentation
2. if it has documentation find the source location where it gets documented

I think 1. should already be there considering drracket can show the documentation of functions etc..

---

<div class="post-metadata">

**Author:** ![capfredf](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/capfredf/32/935_2.png) [@capfredf](https://racket.discourse.group/u/capfredf)\
**Post date:** [February 6, 2022, 8:50pm UTC](https://racket.discourse.group/t/how-to-use-mandatory-arguments-on-the-command-line/655/7 "2022-02-06T20:50:06Z")

</div>

This might be off-topic: I think a flag or option for a command-line program is supposed to be optional.

---

<div class="post-metadata">

**Author:** ![dstorrs](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dstorrs](https://racket.discourse.group/u/dstorrs)\
**Post date:** [February 7, 2022, 4:30pm UTC](https://racket.discourse.group/t/how-to-use-mandatory-arguments-on-the-command-line/655/8 "2022-02-07T16:30:57Z")

</div>

> > ![](https://global.discourse-cdn.com/free1/uploads/racket/original/1X/0e98e36dbadf139e4e258baecda396a6dee3a8fc.png "capfredf") | [capfredf](https://racket.discourse.group/u/capfredf)  
> > February 6 |
> > 
> > - | - |
> 
> This might be off-topic: isn't a flag or option for a command-line program suppose to be optional?

I'm a bit of an iconoclast.
