Skip to content

Commit

Permalink
Convert difficulty to Option[int] and check range
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Apr 21, 2024
1 parent c545937 commit b7c0da8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/cli.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[os, parseutils, strformat, strutils, terminal]
import std/[options, os, parseutils, strformat, strutils, terminal]
import pkg/supersnappy
import patched_libs/parseopt3

Expand Down Expand Up @@ -48,7 +48,7 @@ type
exerciseCreate*: string
offlineCreate*: bool
author*: string
difficulty*: int
difficulty*: Option[int]
of actFmt:
# We can't name these fields `exercise`, `update`, and `yes` because we
# use those names in `actSync`, and Nim doesn't yet support duplicate
Expand Down
16 changes: 13 additions & 3 deletions src/create/create.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[os, strformat]
import std/[options, os, strformat]
import ".."/[cli, helpers, sync/sync, types_track_config]
import "."/[approaches, articles, exercises]

Expand All @@ -13,7 +13,7 @@ proc create*(conf: Conf) =
let msg = &"Both --approach and --article were provided. Please specify only one."
stderr.writeLine msg
quit QuitFailure
if conf.action.difficulty > 0:
if conf.action.difficulty.isSome:
let msg = "The difficulty argument is not supported for approaches"
stderr.writeLine msg
quit QuitFailure
Expand All @@ -40,7 +40,7 @@ proc create*(conf: Conf) =
let msg = "Please specify an exercise to create an article for, using --exercise <slug>"
stderr.writeLine msg
quit QuitFailure
if conf.action.difficulty > 0:
if conf.action.difficulty.isSome:
let msg = "The difficulty argument is not supported for approaches"
stderr.writeLine msg
quit QuitFailure
Expand All @@ -63,8 +63,18 @@ proc create*(conf: Conf) =

createArticle(Slug(conf.action.articleSlug), userExercise, exerciseDir)
elif conf.action.conceptExerciseSlug.len > 0:
if conf.action.difficulty.isSome and conf.action.difficulty.get notin 1..10:
let msg = "The difficulty must be an integer between 1 and 10"
stderr.writeLine msg
quit QuitFailure

createConceptExercise(conf)
elif conf.action.practiceExerciseSlug.len > 0:
if conf.action.difficulty.isSome and conf.action.difficulty.get notin 1..10:
let msg = "The difficulty must be an integer between 1 and 10"
stderr.writeLine msg
quit QuitFailure

createPracticeExercise(conf)
else:
let msg = "Please specify `--practice-exercise <slug>`, `--concept-exercise <slug>`, " &
Expand Down

0 comments on commit b7c0da8

Please sign in to comment.