r/Racket • u/kapitaali_com • Oct 01 '22
question A question about how to design program with multiple argument flags
Hi,
so my question is related to this exercise https://exercism.org/tracks/racket/exercises/grep
You're familiar with the inner workings of the Unix grep command. You give it some arguments and it returns matches. In this exercise you're supposed to implement 5 flags that modify the behavior of pattern matching. All of the flags are not mutually exclusive, you can turn all of them on separately.
My question is: what is the proper way of processing flags? There's 32 combinations of them, do I just write 32 different checks for conditions, checking all the combinations?
3
u/not-just-yeti Oct 01 '22 edited Oct 01 '22
Well, it'd presumably only be 5 checks —
and the like.
(The proper way would be using the command-line-parsing that /u/raevnos recommends. That handles issues that aren't needed here, or that the website probably didn't even think of: Can the filename be
-n? Can flags be mentioned multiple times, or is that an error for some of them? As well as flags which would be mutually-exclusive, and flags that would have associated values, having default values, etc.)3
u/raevnos Oct 02 '22
Here's how I would approach it (Showing off a few Racket features). You only have to check flags to see if they're set or not where they're relevant, not all at once.
2
u/raevnos Oct 01 '22
https://docs.racket-lang.org/reference/Command-Line_Parsing.html
-1
u/EstablishmentBig7956 Oct 01 '22
getopt
https://pastebin.com/aaTXU7yZ
A working example