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

Subcommands #179

Open
leobenkel opened this Issue Jan 3, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@leobenkel
Copy link

leobenkel commented Jan 3, 2019

Hello,

I am confused about the subcommand feature.

Can you have several subCommands ?

like:

class Conf(args: String*) extends ScallopConf(args) {
  val sc = new Subcommand("test") {
    val command = opt[Int]()
  }

  addSubcommand(sc)

  val sc2 = new Subcommand("nextTest" ){
    val command = opt[String]()
  }

  addSubcommand(sc2)
}

?

I tried but it does not seem to work as expected.

My end goal is to have two different behavior for my application.

One with a set of commands A if going the root of the first subCommandA and a set of commands B if going the root of the subCommandB.

Like:

myapp path1 --requiredCommandA ... --requiredCommandB ...

And

myapp path2 --requiredCommandC ... --requiredCommandD ...

Thank you

@Rogach

This comment has been minimized.

Copy link
Member

Rogach commented Jan 4, 2019

Hello!

It seems you forgot to add verify() call after all the options. If you add the verify() call, all seems to work as expected:

import org.rogach.scallop._

class Conf(args: String*) extends ScallopConf(args) {
  val sc = new Subcommand("test") {
    val command = opt[Int]()
  }

  addSubcommand(sc)

  val sc2 = new Subcommand("nextTest" ){
    val command = opt[String]()
  }

  addSubcommand(sc2)

  verify()
}


object Main {
  def main(args: Array[String]) {
    val conf = new Conf(args:_*)
    if (conf.subcommand == Some(conf.sc)) {
      println("test")
      println("command value: " + conf.sc.command())
    } else if (conf.subcommand == Some(conf.sc2)) {
      println("nextTest")
      println("command value: " + conf.sc2.command())
    } else {
      println("no subcommand selected")
    }
  }
}

Ехample outputs:

$ app test --command 1
test
command value: 1
$ app nextTest --command b
nextTest
command value: b
$ app test --command b
[scallop] Error: Bad arguments for option 'command': 'a' - bad Int value
@leobenkel

This comment has been minimized.

Copy link
Author

leobenkel commented Jan 4, 2019

I'll try again. Thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment