Skip to content

Commit

Permalink
Fixed regex for "-" (#237)
Browse files Browse the repository at this point in the history
* Subcommand helpDoc shows all subcommands

* fmt

* fixed bug

* changed output

* fixed

* fixed

* make helpdoc of subcommand list subcommands

* fix regex

* ci

* add test

* fixed ci

* replace unClustered

* fmt

* changed structure

* refactor of validate algorithm

* added merge method

* fixed completion

* added parse for Args

* fixed interface

* fixed argument

* fixed interface

* c

* fixed tests

* added errors

* fixed timeout

* fixed test

* fixed autocorrect

* small error

* fixed some tests and errors

* fixed keyvalue

* fixed some errors

* fixed unclustered

* fixed unclustered2

* test

* check

* check test

* changed test

* fixed key/value

* fixed test

* fixed key/value

* fixed key/value

* fix mistake

* fixed

* fixed match

* fmt

* fixed key/values

* fix case

* added test ambiguity

* fix test

* bump version

* scala 2.12 compatibility

* fmt

* fix redundant

* scala 12

* scala 12

* added docs

* add test

* test

* deleted self

* ci

* fixed merge

* fix merge

* ci

* fixed builtIn

* fix recursion

* fixed recursion

* fixed test

* builtIn tail

* fixed test

* fixed tests

* fixed tests

* fixed test

* fmt

* ci

* fix name

* ci
  • Loading branch information
pablf authored Aug 4, 2023
1 parent 3af13be commit 567a3b5
Show file tree
Hide file tree
Showing 16 changed files with 619 additions and 306 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
fail-fast: false
matrix:
java: [ '11', '17' ]
scala: [ '2.12.17', '2.13.10', '3.3.0' ]
scala: [ '2.12.18', '2.13.11', '3.3.0' ]
platform: [ 'JVM', 'JS', 'Native' ]
steps:
- name: Checkout current branch
Expand Down
4 changes: 2 additions & 2 deletions project/BuildHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import scalafix.sbt.ScalafixPlugin.autoImport._

object BuildHelper {

val Scala212 = "2.12.17"
val Scala213 = "2.13.10"
val Scala212 = "2.12.18"
val Scala213 = "2.13.11"
val Scala3 = "3.3.0"

val SilencerVersion = "1.17.13"
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.5.9")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.10.3")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.11.0")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0")
addSbtPlugin("com.github.sbt" % "sbt-unidoc" % "0.5.0")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.11")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ private[cli] object OAuth2PlatformSpecific {
provider: OAuth2Provider,
scope: List[String],
auxiliaryOptions: Options[OAuth2AuxiliaryOptions],
args: List[String],
args: Predef.Map[String, List[String]],
conf: CliConfig
): IO[ValidationError, (List[String], OAuth2Token)] =
): IO[ValidationError, OAuth2Token] =
ZIO.dieMessage(
"OAuth2 support is not currently implemented for Scala.js. If you are interested in adding support, please open a pull request at https://github.com/zio/zio-cli."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ private[cli] object OAuth2PlatformSpecific {
provider: OAuth2Provider,
scope: List[String],
auxiliaryOptions: Options[OAuth2AuxiliaryOptions],
args: List[String],
args: Predef.Map[String, List[String]],
conf: CliConfig
): IO[ValidationError, (List[String], OAuth2Token)] =
auxiliaryOptions.validate(args, conf).flatMap { case (args, aux) =>
): IO[ValidationError, OAuth2Token] =
auxiliaryOptions.validate(args, conf).flatMap { case aux =>
new OAuth2(provider, aux.file, scope).loadOrAuthorize
.mapError(ex => ValidationError(ValidationErrorType.InvalidValue, p(ex.getMessage)))
.map(token => (args, token))
}

def findProvider(opt: Options[Any]): Option[OAuth2Provider] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ object FigFontRenderReportSpec extends ZIOSpecDefault {
val path = Files.createTempFile("figlet-report", ".md")
(path, Files.newBufferedWriter(path))
}
} { case (_, w) =>
} { _ =>
ZIO.unit
} { case (path, w) =>
ZIO.attempt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ object FigFontRendererJvmSpec extends ZIOSpecDefault {
def spec = suite("FigFontRendererJvmSpec")(
test("ZIO-CLI!!! with standard.flf") {
for {
font_ <- FigFont.fromResource("standard.flf", getClass.getClassLoader)
font: FigFont = font_ // TODO IJ cross-platform projects issue
r = render(font, "ZIO-\nCLI!!!")
font <- FigFont.fromResource("standard.flf", getClass.getClassLoader)
r = render(font, "ZIO-\nCLI!!!")
} yield {
assertTextBlock(
r,
Expand Down
6 changes: 6 additions & 0 deletions zio-cli/shared/src/main/scala/zio/cli/Args.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ object Args {

lazy val synopsis: UsageSynopsis = UsageSynopsis.Named(List(name), primType.choices)

override def parse(args: List[String], conf: CliConfig): IO[ValidationError, (List[String], List[String])] =
ZIO.succeed((Nil, Nil))

def validate(args: List[String], conf: CliConfig): IO[ValidationError, (List[String], A)] =
(args match {
case head :: tail => primType.validate(Some(head), conf).mapBoth(text => HelpDoc.p(text), a => tail -> a)
Expand Down Expand Up @@ -191,6 +194,9 @@ object Args {

lazy val minSize: Int = min.getOrElse(0) * value.minSize

override def parse(args: List[String], conf: CliConfig): IO[ValidationError, (List[String], List[String])] =
ZIO.succeed((Nil, Nil))

def validate(args: List[String], conf: CliConfig): IO[ValidationError, (List[String], List[A])] = {
val min1 = min.getOrElse(0)
val max1 = max.getOrElse(Int.MaxValue)
Expand Down
5 changes: 3 additions & 2 deletions zio-cli/shared/src/main/scala/zio/cli/CliConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import zio.{URIO, ZIO}
*/
final case class CliConfig(
caseSensitive: Boolean,
autoCorrectLimit: Int
autoCorrectLimit: Int,
finalCheckBuiltIn: Boolean = true
) {
def normalizeCase(text: String): String = if (caseSensitive) text else text.toLowerCase()

Expand All @@ -28,7 +29,7 @@ object CliConfig {
/**
* The default options are case sensitive parsing
*/
val default: CliConfig = CliConfig(caseSensitive = false, autoCorrectLimit = 2)
val default: CliConfig = CliConfig(caseSensitive = false, autoCorrectLimit = 2, finalCheckBuiltIn = true)

val cliConfig: URIO[CliConfig, CliConfig] = ZIO.service
}
70 changes: 48 additions & 22 deletions zio-cli/shared/src/main/scala/zio/cli/Command.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,6 @@ sealed trait Command[+A] extends Parameter with Named { self =>
}

object Command {
def unCluster(args: List[String]): List[String] = {
def isClusteredOption(value: String): Boolean = value.trim.matches("^-{1}([^-]{2,}|$)")

args.flatMap { arg =>
if (isClusteredOption(arg))
arg.substring(1).map(c => s"-$c")
else arg :: Nil
}
}

private def splitForcedArgs(args: List[String]): (List[String], List[String]) = {
val (remainingArgs, forcedArgs) = args.span(_ != "--")
Expand Down Expand Up @@ -121,15 +112,27 @@ object Command {
args: List[String],
conf: CliConfig
): IO[ValidationError, CommandDirective[(OptionsType, ArgsType)]] = {
val parseBuiltInArgs =
if (args.headOption.exists(conf.normalizeCase(_) == conf.normalizeCase(name)))
BuiltInOption
.builtInOptions(self, synopsis, helpDoc)
.validate(args, conf)
.mapBoth(_.error, _._2)
.some
def parseBuiltInArgs(args: List[String]): IO[ValidationError, CommandDirective[Nothing]] =
if (args.headOption.exists(conf.normalizeCase(_) == conf.normalizeCase(self.name))) {
val options = BuiltInOption
.builtInOptions(self, self.synopsis, self.helpDoc)
Options
.validate(options, args.tail, conf)
.map(_._3)
.someOrFail(
ValidationError(
ValidationErrorType.NoBuiltInMatch,
HelpDoc.p(s"No built-in option was matched")
)
)
.map(CommandDirective.BuiltIn)
else ZIO.fail(None)
} else
ZIO.fail(
ValidationError(
ValidationErrorType.CommandMismatch,
HelpDoc.p(s"Missing command name: ${name}")
)
)

val parseUserDefinedArgs =
for {
Expand All @@ -155,13 +158,36 @@ object Command {
}
tuple1 = splitForcedArgs(commandOptionsAndArgs)
(optionsAndArgs, forcedCommandArgs) = tuple1
tuple2 <- options.validate(unCluster(optionsAndArgs), conf)
(commandArgs, optionsType) = tuple2
tuple <- self.args.validate(commandArgs ++ forcedCommandArgs, conf)
(argsLeftover, argsType) = tuple
tuple2 <- Options.validate(options, optionsAndArgs, conf)
(error, commandArgs, optionsType) = tuple2
tuple <- self.args.validate(commandArgs ++ forcedCommandArgs, conf).catchSome { case e =>
error match {
case None => ZIO.fail(e)
case Some(err) => ZIO.fail(err)
}
}
(argsLeftover, argsType) = tuple
} yield CommandDirective.userDefined(argsLeftover, (optionsType, argsType))

parseBuiltInArgs orElse parseUserDefinedArgs
val exhaustiveSearch: IO[ValidationError, CommandDirective[(OptionsType, ArgsType)]] =
if (args.contains("--help") || args.contains("-h")) parseBuiltInArgs(List(name, "--help"))
else if (args.contains("--wizard") || args.contains("-w")) parseBuiltInArgs(List(name, "--wizard"))
else
ZIO.fail(
ValidationError(
ValidationErrorType.CommandMismatch,
HelpDoc.p(s"Missing command name: ${name}")
)
)

val first = parseBuiltInArgs(args) orElse parseUserDefinedArgs

first.catchSome { case e: ValidationError =>
if (conf.finalCheckBuiltIn) exhaustiveSearch.catchSome { case _: ValidationError =>
ZIO.fail(e)
}
else ZIO.fail(e)
}
}

lazy val synopsis: UsageSynopsis =
Expand Down
Loading

0 comments on commit 567a3b5

Please sign in to comment.