Skip to content
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

access to other args in validation functions #27

Closed
zero-sum opened this issue Oct 30, 2013 · 7 comments
Closed

access to other args in validation functions #27

zero-sum opened this issue Oct 30, 2013 · 7 comments

Comments

@zero-sum
Copy link

Is there a way to allow validation functions access to other arguments?

For instance, if there is an arg specifying an "output file" I could evaluate it more accurately during validation if I was aware that option "--overwriteExisting" were also set.

@eed3si9n
Copy link
Member

eed3si9n commented Nov 4, 2013

How would it look like?

arg[File]("<file>...") unbounded() optional() action { (x, c) =>
    c.copy(files = c.files :+ x) } text("optional unbounded args") validateWithConfig { (x, c) =>
      if (c.overwriteExisting) failure("The file doesn't exist")
      else success
    }

Something like the above?

@zero-sum
Copy link
Author

zero-sum commented Nov 4, 2013

Perfect!

Only one question -- could precedence (e.g. the arg depends on the opt already having been parsed) be handled simply by the order in which they are defined in the parser declaration? Or would some sort of internal DAG need to be used.

@eed3si9n
Copy link
Member

eed3si9n commented Nov 5, 2013

The way it works now is that they are processed in the order it was passed, so you'd need to use Option[T] or something to track options and validate them when all are filled in.

@zero-sum
Copy link
Author

zero-sum commented Nov 5, 2013

Not exactly sure how this would work. Maybe I am misunderstanding something?

".. -overwriteExisting <file> .."
-overwriteExisting will be set to either None or Some[Boolean], enabling validation of <file>
".. <file> -overwriteExisting .." // preferred usage
-overwriteExisting will not be set at the time of file validation. <file> could be validated during -overwriteExisting validation, but only if -overwriteExisting (which is optional) is used

I can think of a couple solutions:

  • Provide the ability for a final validation pass after all args have been parsed
  • Define -overwriteExisting as a child of <file>, and then enforce processing of of children before processing of parent. e.g.
arg[File]("<output path>")
  .text("The output location")
  .children(
    opt[Unit]("overwrite") 
    abbr("o")   
    action { (_, c) => c.copy(overwriteOutputPath = false) } 
    text("overwrite output path")
  )
  .action { (x, c) => c.copy(outputPath = x) }
  .validateWithConfig{ (x,c) => // perform file validation using c.overwriteExisting }

@eed3si9n
Copy link
Member

Added a standalone checkConfig construct. This can also be nested within children.

val parser = new scopt.OptionParser[Config]("scopt") {
  head("scopt", "3.x")
  opt[Unit]('k', "keepalive") action { (x, c) =>
    c.copy(keepalive= true) }
  opt[Boolean]("xyz") action { (x, c) =>
    c.copy(xyz = x) } text("xyz is a boolean property"),
  checkConfig { c =>
    if (c.keepalive && c.xyz) failure("xyz cannot keep alive") else success }
}

It's available as 3.2.0-SNAPSHOT if you wanna give it a shot.

@eed3si9n
Copy link
Member

3.2.0 is out.

@zero-sum
Copy link
Author

Thanks, works great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants