Skip to content

Commit

Permalink
Merge pull request #5185 from unisonweb/arya/commandline-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
aryairani committed Jul 5, 2024
2 parents 293b4a2 + e102b0d commit 222bd48
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
17 changes: 9 additions & 8 deletions unison-cli/src/Unison/CommandLine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import Unison.CommandLine.FuzzySelect qualified as Fuzzy
import Unison.CommandLine.Helpers (warn)
import Unison.CommandLine.InputPattern (InputPattern (..))
import Unison.CommandLine.InputPattern qualified as InputPattern
import Unison.CommandLine.InputPatterns qualified as IPs
import Unison.Parser.Ann (Ann)
import Unison.Prelude
import Unison.Project.Util (ProjectContext, projectContextFromPath)
Expand Down Expand Up @@ -117,12 +118,10 @@ parseInput codebase currentPath numberedArgs patterns segments = runExceptT do
<> P.newline
<> P.newline
<> P.wrap
( P.text $
"You can run `help "
<> Text.pack command
<> "` for more information on using `"
<> Text.pack command
<> "`."
( "You can run"
<> IPs.makeExample IPs.help [fromString command]
<> "for more information on using"
<> IPs.makeExampleEOS pat []
)
)
$ parse resolvedArgs
Expand All @@ -131,9 +130,11 @@ parseInput codebase currentPath numberedArgs patterns segments = runExceptT do
throwE
. warn
. P.wrap
$ "I don't know how to "
$ "I don't know how to"
<> P.group (fromString command <> ".")
<> "Type `help` or `?` to get help."
<> "Type"
<> IPs.makeExample' IPs.help
<> "or `?` to get help."
where
noCompletionsMessage argDesc =
P.callout "⚠️" $
Expand Down
49 changes: 24 additions & 25 deletions unison-cli/src/Unison/CommandLine/InputPatterns.hs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ import Unison.CommandLine.BranchRelativePath (BranchRelativePath (..), parseBran
import Unison.CommandLine.BranchRelativePath qualified as BranchRelativePath
import Unison.CommandLine.Completion
import Unison.CommandLine.FZFResolvers qualified as Resolvers
import Unison.CommandLine.Helpers (aside, backtick, tip, warn)
import Unison.CommandLine.Helpers (aside, backtick, tip)
import Unison.CommandLine.InputPattern (ArgumentType (..), InputPattern (InputPattern), IsOptional (..), unionSuggestions)
import Unison.CommandLine.InputPattern qualified as I
import Unison.Core.Project (ProjectBranchName (..))
Expand Down Expand Up @@ -300,14 +300,13 @@ searchResultToHQ oprefix = \case
addPrefix :: Name -> Name
addPrefix = maybe id Path.prefixNameIfRel oprefix

unsupportedStructuredArgument :: Text -> Text -> I.Argument -> Either (P.Pretty CT.ColorText) String
unsupportedStructuredArgument :: InputPattern -> Text -> I.Argument -> Either (P.Pretty CT.ColorText) String
unsupportedStructuredArgument command expected =
either pure . const . Left . P.text $
"`"
<> command
<> "` can’t accept a numbered argument for "
<> expected
<> " and it’s not yet possible to provide un-expanded numbers as arguments."
either pure . const . Left . P.wrap $
makeExample' command
<> "can’t accept a numbered argument for"
<> P.text expected
<> "and it’s not yet possible to provide un-expanded numbers as arguments."

expectedButActually' :: Text -> String -> P.Pretty CT.ColorText
expectedButActually' expected actualValue =
Expand Down Expand Up @@ -793,7 +792,7 @@ load =
)
\case
[] -> pure $ Input.LoadI Nothing
[file] -> Input.LoadI . Just <$> unsupportedStructuredArgument "load" "a file name" file
[file] -> Input.LoadI . Just <$> unsupportedStructuredArgument load "a file name" file
args -> wrongArgsLength "no more than one argument" args

clear :: InputPattern
Expand Down Expand Up @@ -1016,7 +1015,7 @@ displayTo =
(wrongArgsLength "at least two arguments" [file])
( \defs ->
Input.DisplayI . Input.FileLocation
<$> unsupportedStructuredArgument "display.to" "a file name" file
<$> unsupportedStructuredArgument displayTo "a file name" file
<*> traverse handleHashQualifiedNameArg defs
)
$ NE.nonEmpty defs
Expand Down Expand Up @@ -1823,7 +1822,7 @@ debugTabCompletion =
P.wrap $ "Completions which are finished are prefixed with a * represent finished completions."
]
)
(fmap Input.DebugTabCompletionI . traverse (unsupportedStructuredArgument "debug.tab-complete" "text"))
(fmap Input.DebugTabCompletionI . traverse (unsupportedStructuredArgument debugTabCompletion "text"))

debugLspNameCompletion :: InputPattern
debugLspNameCompletion =
Expand All @@ -1837,8 +1836,8 @@ debugLspNameCompletion =
]
)
\case
[prefix] -> Input.DebugLSPNameCompletionI . Text.pack <$> unsupportedStructuredArgument "text" prefix
_ -> Left (I.help debugLspNameCompletion)
[prefix] -> Input.DebugLSPNameCompletionI . Text.pack <$> unsupportedStructuredArgument debugLspNameCompletion "text" prefix
args -> wrongArgsLength "exactly one argument" args

debugFuzzyOptions :: InputPattern
debugFuzzyOptions =
Expand All @@ -1858,8 +1857,8 @@ debugFuzzyOptions =
\case
(cmd : args) ->
Input.DebugFuzzyOptionsI
<$> unsupportedStructuredArgument "debug.fuzzy-options" "a command" cmd
<*> traverse (unsupportedStructuredArgument "debug.fuzzy-options" "text") args
<$> unsupportedStructuredArgument debugFuzzyOptions "a command" cmd
<*> traverse (unsupportedStructuredArgument debugFuzzyOptions "text") args
args -> wrongArgsLength "at least one argument" args

debugFormat :: InputPattern
Expand Down Expand Up @@ -2345,7 +2344,7 @@ helpTopics =
( \case
[] -> Right $ Input.CreateMessage topics
[topic] -> do
topic <- unsupportedStructuredArgument "help-topics" "a help topic" topic
topic <- unsupportedStructuredArgument helpTopics "a help topic" topic
case Map.lookup topic helpTopicsMap of
Nothing -> Left $ "I don't know of that topic. Try `help-topics`."
Just t -> Right $ Input.CreateMessage t
Expand Down Expand Up @@ -2534,10 +2533,10 @@ help =
showPatternHelp
visibleInputs
[cmd] -> do
cmd <- unsupportedStructuredArgument "help" "a command" cmd
cmd <- unsupportedStructuredArgument help "a command" cmd
case (Map.lookup cmd commandsByName, isHelp cmd) of
(Nothing, Just msg) -> Right $ Input.CreateMessage msg
(Nothing, Nothing) -> Left $ "I don't know of that command. Try `help`."
(Nothing, Nothing) -> Left $ "I don't know of that command. Try" <> makeExampleEOS help []
(Just pat, Nothing) -> Right . Input.CreateMessage $ showPatternHelp pat
-- If we have a command and a help topic with the same name (like "projects"), then append a tip to the
-- command's help that suggests running `help-topic command`
Expand Down Expand Up @@ -2816,7 +2815,7 @@ docsToHtml =
[namespacePath, destinationFilePath] ->
Input.DocsToHtmlI
<$> handleBranchRelativePathArg namespacePath
<*> unsupportedStructuredArgument "docs.to-html" "a file name" destinationFilePath
<*> unsupportedStructuredArgument docsToHtml "a file name" destinationFilePath
args -> wrongArgsLength "exactly two arguments" args

docToMarkdown :: InputPattern
Expand Down Expand Up @@ -2856,7 +2855,7 @@ execute =
main : args ->
Input.ExecuteI
<$> handleHashQualifiedNameArg main
<*> traverse (unsupportedStructuredArgument "run" "a command-line argument") args
<*> traverse (unsupportedStructuredArgument execute "a command-line argument") args
[] -> wrongArgsLength "at least one argument" []

saveExecuteResult :: InputPattern
Expand Down Expand Up @@ -2927,7 +2926,7 @@ makeStandalone =
$ \case
[main, file] ->
Input.MakeStandaloneI
<$> unsupportedStructuredArgument "compile" "a file name" file
<$> unsupportedStructuredArgument makeStandalone "a file name" file
<*> handleHashQualifiedNameArg main
args -> wrongArgsLength "exactly two arguments" args

Expand All @@ -2948,7 +2947,7 @@ runScheme =
main : args ->
Input.ExecuteSchemeI
<$> handleHashQualifiedNameArg main
<*> traverse (unsupportedStructuredArgument "run.native" "a command-line argument") args
<*> traverse (unsupportedStructuredArgument runScheme "a command-line argument") args
[] -> wrongArgsLength "at least one argument" []

compileScheme :: InputPattern
Expand All @@ -2969,7 +2968,7 @@ compileScheme =
$ \case
[main, file] ->
Input.CompileSchemeI . Text.pack
<$> unsupportedStructuredArgument "compile.native" "a file name" file
<$> unsupportedStructuredArgument compileScheme "a file name" file
<*> handleHashQualifiedNameArg main
args -> wrongArgsLength "exactly two arguments" args

Expand Down Expand Up @@ -2997,7 +2996,7 @@ createAuthor =
<$> handleRelativeNameSegmentArg symbolStr
<*> fmap
(parseAuthorName . unwords)
(traverse (unsupportedStructuredArgument "create.author" "text") authorStr)
(traverse (unsupportedStructuredArgument createAuthor "text") authorStr)
args -> wrongArgsLength "at least two arguments" args
where
-- let's have a real parser in not too long
Expand Down Expand Up @@ -3265,7 +3264,7 @@ releaseDraft =
bimap (const "Couldn’t parse version number") Input.ReleaseDraftI
. tryInto @Semver
. Text.pack
=<< unsupportedStructuredArgument "release.draft" "a version number" semverString
=<< unsupportedStructuredArgument releaseDraft "a version number" semverString
args -> wrongArgsLength "exactly one argument" args
}

Expand Down

0 comments on commit 222bd48

Please sign in to comment.