Skip to content

Commit

Permalink
cli: work towards generate being consistent with sync and fmt
Browse files Browse the repository at this point in the history
Remaining is just to alter `generate.nim` and update the tests accordingly.

Refs: 542
  • Loading branch information
ee7 committed Jun 19, 2022
1 parent d39169d commit 8a64701
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ Options for fmt:
-u, --update Prompt to write formatted files
-y, --yes Auto-confirm the prompt from --update
Options for generate:
-e, --exercise <slug> Only operate on this exercise
-u, --update Prompt to write generated files
-y, --yes Auto-confirm the prompt from --update
Options for info:
-o, --offline Do not update the cached 'problem-specifications' data
Expand Down
72 changes: 45 additions & 27 deletions src/cli.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type

Action* = object
case kind*: ActionKind
of actNil, actGenerate, actLint:
of actNil, actLint:
discard
of actFmt:
# We can't name these fields `exercise`, `update`, and `yes` because we
Expand All @@ -33,6 +33,10 @@ type
exerciseFmt*: string
updateFmt*: bool
yesFmt*: bool
of actGenerate:
exerciseGenerate*: string
updateGenerate*: bool
yesGenerate*: bool
of actInfo:
offlineInfo*: bool
of actSync:
Expand Down Expand Up @@ -62,10 +66,10 @@ type
optTrackDir = "trackDir"
optVerbosity = "verbosity"

# Options for both `fmt` and `sync`
optFmtSyncExercise = "exercise"
optFmtSyncUpdate = "update"
optFmtSyncYes = "yes"
# Options for `fmt`, `generate` and `sync`
optFmtGenerateSyncExercise = "exercise"
optFmtGenerateSyncUpdate = "update"
optFmtGenerateSyncYes = "yes"

# Options for both `info` and `sync`
optInfoSyncOffline = "offline"
Expand All @@ -92,7 +96,7 @@ const
repoRootDir = currentSourcePath().parentDir().parentDir()
configletVersion = staticRead(repoRootDir / "configlet.version").strip()
short = genShortKeys()
optsNoVal = {optHelp, optVersion, optFmtSyncUpdate, optFmtSyncYes,
optsNoVal = {optHelp, optVersion, optFmtGenerateSyncUpdate, optFmtGenerateSyncYes,
optInfoSyncOffline, optSyncDocs, optSyncFilepaths, optSyncMetadata}

func generateNoVals: tuple[shortNoVal: set[char], longNoVal: seq[string]] =
Expand Down Expand Up @@ -147,7 +151,7 @@ func genHelpText: string =
case opt
of optTrackDir: "dir"
of optVerbosity: "verbosity"
of optFmtSyncExercise: "slug"
of optFmtGenerateSyncExercise: "slug"
of optSyncTests: "mode"
of optUuidNum: "int"
else: ""
Expand Down Expand Up @@ -199,15 +203,15 @@ func genHelpText: string =
optTrackDir: "Specify a track directory to use instead of the current directory",
optVerbosity: &"The verbosity of output.\n" &
&"{paddingOpt}{allowedValues(Verbosity)} (default: normal)",
optFmtSyncExercise: "Only operate on this exercise",
optFmtSyncUpdate: "Prompt to update the unsynced track data",
optFmtSyncYes: &"Auto-confirm prompts from --{$optFmtSyncUpdate} for updating docs, filepaths, and metadata",
optFmtGenerateSyncExercise: "Only operate on this exercise",
optFmtGenerateSyncUpdate: "Prompt to update the unsynced track data",
optFmtGenerateSyncYes: &"Auto-confirm prompts from --{$optFmtGenerateSyncUpdate} for updating docs, filepaths, and metadata",
optInfoSyncOffline: "Do not update the cached 'problem-specifications' data",
optSyncDocs: "Sync Practice Exercise '.docs/introduction.md' and '.docs/instructions.md' files",
optSyncFilepaths: "Populate empty 'files' values in Concept/Practice exercise '.meta/config.json' files",
optSyncMetadata: "Sync Practice Exercise '.meta/config.json' metadata values",
optSyncTests: &"Sync Practice Exercise '.meta/tests.toml' files.\n" &
&"{paddingOpt}The mode value specifies how missing tests are handled when using --{$optFmtSyncUpdate}.\n" &
&"{paddingOpt}The mode value specifies how missing tests are handled when using --{$optFmtGenerateSyncUpdate}.\n" &
&"{paddingOpt}{allowedValues(TestsMode)} (default: choose)",
optUuidNum: "Number of UUIDs to output",
]
Expand Down Expand Up @@ -249,22 +253,26 @@ func genHelpText: string =
elif key != "kind":
let opt =
case key
of "exerciseFmt":
optFmtSyncExercise
of "updateFmt":
optFmtSyncUpdate
of "yesFmt":
optFmtSyncYes
of "exerciseFmt", "exerciseGenerate":
optFmtGenerateSyncExercise
of "updateFmt", "updateGenerate":
optFmtGenerateSyncUpdate
of "yesFmt", "yesGenerate":
optFmtGenerateSyncYes
of "offlineInfo":
optInfoSyncOffline
else:
parseEnum[Opt](key)
# Set the description for `fmt` options.
let desc =
if actionKind == actFmt and opt == optFmtSyncUpdate:
if actionKind == actFmt and opt == optFmtGenerateSyncUpdate:
"Prompt to write formatted files"
elif actionKind == actFmt and opt == optFmtSyncYes:
&"Auto-confirm the prompt from --{$optFmtSyncUpdate}"
elif actionKind == actFmt and opt == optFmtGenerateSyncYes:
&"Auto-confirm the prompt from --{$optFmtGenerateSyncUpdate}"
elif actionKind == actGenerate and opt == optFmtGenerateSyncUpdate:
"Prompt to write generated files"
elif actionKind == actGenerate and opt == optFmtGenerateSyncYes:
&"Auto-confirm the prompt from --{$optFmtGenerateSyncUpdate}"
else:
optionDescriptions[opt]
result.add alignLeft(syntax[opt], maxLen) & desc & "\n"
Expand Down Expand Up @@ -461,18 +469,28 @@ proc handleOption(conf: var Conf; kind: CmdLineKind; key, val: string) =
# Process action-specific options
if not isGlobalOpt:
case conf.action.kind
of actNil, actGenerate, actLint:
of actNil, actLint:
discard
of actFmt:
case opt
of optFmtSyncExercise:
of optFmtGenerateSyncExercise:
setActionOpt(exerciseFmt, val)
of optFmtSyncUpdate:
of optFmtGenerateSyncUpdate:
setActionOpt(updateFmt, true)
of optFmtSyncYes:
of optFmtGenerateSyncYes:
setActionOpt(yesFmt, true)
else:
discard
of actGenerate:
case opt
of optFmtGenerateSyncExercise:
setActionOpt(exerciseGenerate, val)
of optFmtGenerateSyncUpdate:
setActionOpt(updateGenerate, true)
of optFmtGenerateSyncYes:
setActionOpt(yesGenerate, true)
else:
discard
of actInfo:
case opt
of optInfoSyncOffline:
Expand All @@ -481,11 +499,11 @@ proc handleOption(conf: var Conf; kind: CmdLineKind; key, val: string) =
discard
of actSync:
case opt
of optFmtSyncExercise:
of optFmtGenerateSyncExercise:
setActionOpt(exercise, val)
of optFmtSyncUpdate:
of optFmtGenerateSyncUpdate:
setActionOpt(update, true)
of optFmtSyncYes:
of optFmtGenerateSyncYes:
setActionOpt(yes, true)
of optSyncTests:
setActionOpt(tests, parseVal[TestsMode](kind, key, val))
Expand Down
4 changes: 2 additions & 2 deletions src/sync/sync.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ proc validate(conf: Conf) =
if conf.action.update:
if conf.action.yes and skTests in conf.action.scope:
let msg = fmt"""
'{list(optFmtSyncYes)}' was provided to non-interactively update, but the tests updating mode is still 'choose'.
'{list(optFmtGenerateSyncYes)}' was provided to non-interactively update, but the tests updating mode is still 'choose'.
You can either:
- remove '{list(optFmtSyncYes)}', and update by confirming prompts
- remove '{list(optFmtGenerateSyncYes)}', and update by confirming prompts
- or narrow the syncing scope via some combination of --docs, --filepaths, and --metadata
- or add '--tests include' or '--tests exclude' to non-interactively include/exclude missing tests
If no syncing scope option is provided, configlet uses the full syncing scope.
Expand Down

0 comments on commit 8a64701

Please sign in to comment.