Saurabh Nanda
Saurabh Nanda

Reputation: 6793

Generate command line strings using optparse-applicative

Given a Parser a and a value of type a is it possible to generate the relevant command-line (in textual format)? (Basically, the exact reverse of what optparse-applicative is generally used for!)

For example, given something like...

data Args = {userName :: Text, userGroups :: Text }

parser :: Parser Args
parser = Args
  <$> (strOption $ long "name")
  <*> (many $ strOption $ long "group")

...how does one convert the following...

let args = Args { userName :: "testUser", userGroups :: ["system", "sudo"] }

...to...

--name=testUser --group=system --group=sudo

Upvotes: 1

Views: 118

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 153132

No, there is no way in general. The relevant bits are:

data Parser a
  = forall x . MultP (Parser (x -> a)) (Parser x)
  | forall x . BindP (Parser x) (x -> Parser a)
  | -- ...

Since the xs of MultP and BindP are existentially quantified, the information about the suitable xs that could be used to produce your a is lost at runtime.

Upvotes: 1

Related Questions