-
Notifications
You must be signed in to change notification settings - Fork 22
A more precise way to specify flags #1133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE DuplicateRecordFields #-} | ||
| {-# LANGUAGE FlexibleInstances #-} | ||
| {-# LANGUAGE LambdaCase #-} | ||
| {-# LANGUAGE TypeApplications #-} | ||
| {-# LANGUAGE TypeOperators #-} | ||
|
|
||
| module Cardano.CLI.Option.Flag | ||
| ( Flag (Flag) | ||
| , FlagOptions (FlagOptions) | ||
| , Defaultness (..) | ||
| , setDefault | ||
| , mkFlag | ||
| , parserFromFlags | ||
| ) | ||
| where | ||
|
|
||
| import Cardano.CLI.Option.Flag.Type | ||
| ( Defaultness (IsDefault) | ||
| , Flag (Flag) | ||
| , FlagOptions (FlagOptions) | ||
| , defaultFlagOptions | ||
| ) | ||
| import Cardano.CLI.Option.Flag.Type qualified as Z | ||
| import Cardano.CLI.Vary | ||
| import Cardano.CLI.Vary qualified as Vary | ||
|
|
||
| import Control.Applicative | ||
| import Data.Function | ||
| import Data.Generics.Product.Any | ||
| import Lens.Micro | ||
| import Options.Applicative (Parser) | ||
| import Options.Applicative qualified as Opt | ||
|
|
||
| -- | Create a parser from a help rendering function and list of flags. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice haddock 👍 |
||
| -- A default parser is included at the end of parser alternatives for | ||
| -- the default flag (there should only be one default, but if more than | ||
| -- one is specified, the first such one is used as the default). | ||
| parserFromFlags :: (Flag a -> String) -> [Flag a] -> Parser a | ||
| parserFromFlags _ [] = empty | ||
| parserFromFlags mkHelp fs = | ||
| alternatives fs <|> defaults fs | ||
| where | ||
| alternatives [] = empty | ||
| alternatives (x : xs) = | ||
| parserFromFlag mkHelp x <|> alternatives xs | ||
|
|
||
| defaults :: [Flag a] -> Parser a | ||
| defaults = \case | ||
| [] -> empty | ||
| (x : xs) | flagIsDefault x -> parserFromFlagDefault x <|> defaults xs | ||
| (_ : xs) -> defaults xs | ||
|
|
||
| flagIsDefault :: Flag a -> Bool | ||
| flagIsDefault flag = | ||
| Z.isDefault (Z.options flag) == Z.IsDefault | ||
|
|
||
| parserFromFlag :: (Flag a -> String) -> Flag a -> Parser a | ||
| parserFromFlag mkHelp flag = | ||
| Opt.flag' (flag & Z.value) $ | ||
| mconcat | ||
| [ Opt.long $ flag & Z.longName | ||
| , Opt.help $ mkHelp flag | ||
| ] | ||
|
|
||
| parserFromFlagDefault :: Flag a -> Parser a | ||
| parserFromFlagDefault flag = | ||
| pure $ flag & Z.value | ||
|
|
||
| mkFlag | ||
| :: a :| fs | ||
| => String | ||
| -> String | ||
| -> a | ||
| -> Flag (Vary fs) | ||
| mkFlag longName format value = | ||
| Flag longName format defaultFlagOptions (Vary.from value) | ||
|
|
||
| setDefault :: Flag a -> Flag a | ||
| setDefault flag = | ||
| flag & the @"options" . the @"isDefault" .~ IsDefault | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE DeriveGeneric #-} | ||
| {-# LANGUAGE DuplicateRecordFields #-} | ||
| {-# LANGUAGE FlexibleInstances #-} | ||
|
|
||
| module Cardano.CLI.Option.Flag.Type | ||
| ( Flag (..) | ||
| , Defaultness (..) | ||
| , FlagOptions (..) | ||
| , defaultFlagOptions | ||
| ) | ||
| where | ||
|
|
||
| import GHC.Generics | ||
|
|
||
| data Defaultness | ||
| = IsDefault | ||
| | NonDefault | ||
| deriving (Show, Eq, Generic) | ||
|
|
||
| -- | Options for a flag that control how it is rendered and parsed. | ||
| newtype FlagOptions = FlagOptions | ||
| { isDefault :: Defaultness | ||
| -- ^ Whether the flag is a default value. | ||
| } | ||
| deriving (Show, Eq, Generic) | ||
|
|
||
| -- instance Semigroup FlagOptions where | ||
| -- FlagOptions IsDefault <> FlagOptions _ = FlagOptions IsDefault | ||
| -- FlagOptions _ <> FlagOptions IsDefault = FlagOptions IsDefault | ||
| -- FlagOptions _ <> FlagOptions _ = FlagOptions NonDefault | ||
|
|
||
| -- instance Monoid FlagOptions where | ||
| -- mempty = FlagOptions NonDefault | ||
| -- mappend = (<>) | ||
|
|
||
| -- | A flag that can be used in the command line interface. | ||
| -- | ||
| -- It contains information about how to render the flag, its long name, | ||
| -- its content, and its value. | ||
| -- The type variable 'a' represents the type of the value that the flag holds. | ||
| -- | ||
| -- The reason for this type instead of using 'Parser a' directly is to | ||
| -- allow for more complex parsing logic, such as handling default values. | ||
| data Flag a = Flag | ||
| { longName :: String | ||
| , format :: String | ||
| , options :: FlagOptions | ||
| , value :: a | ||
| } | ||
| deriving (Show, Eq, Generic) | ||
|
|
||
| defaultFlagOptions :: FlagOptions | ||
| defaultFlagOptions = FlagOptions NonDefault |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can still have flexibility on the help for the flag by providing a rendering function like this.
parserFromFlagsoffers that flexibility andparserFromFormatFlagstakes that flexibility away so we can be consistent.