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

ShowHelpException not caught #20

Closed
passy opened this issue Jun 28, 2017 · 7 comments
Closed

ShowHelpException not caught #20

passy opened this issue Jun 28, 2017 · 7 comments

Comments

@passy
Copy link

passy commented Jun 28, 2017

Apologies if I'm misunderstanding something here, but from looking at the implementation it doesn't seem like anything is catching com.xenomachina.argparser.ShowHelpException: Help was requested and if I start my application with -h or --help, I see the exception stacktrace rather than the help text:

Exception in thread "main" com.xenomachina.argparser.ShowHelpException: Help was requested
        at com.xenomachina.argparser.ArgParser$1.invoke(ArgParser.kt:590)
        at com.xenomachina.argparser.ArgParser$1.invoke(ArgParser.kt:38)
        at com.xenomachina.argparser.OptionDelegate.parseOption(OptionDelegate.kt:60)
        at com.xenomachina.argparser.ArgParser.parseShortOpts(ArgParser.kt:576)
        at com.xenomachina.argparser.ArgParser.access$parseShortOpts(ArgParser.kt:38)
        at com.xenomachina.argparser.ArgParser$parseOptions$2.invoke(ArgParser.kt:473)
        at com.xenomachina.argparser.ArgParser$parseOptions$2.invoke(ArgParser.kt:38)
        at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:130)
        at com.xenomachina.argparser.ArgParser.getParseOptions(ArgParser.kt)
        at com.xenomachina.argparser.ArgParser.force(ArgParser.kt:447)
        at com.xenomachina.argparser.ParsingDelegate.getValue(ParsingDelegate.kt:39)
        at com.xenomachina.argparser.ArgParser$Delegate.getValue(ArgParser.kt:340)
        at myapp.TestRunnerArgs.getTests(testRunner.kt)

Is this expected? Do I need to catch this myself and delegate to the help formatter?

@passy
Copy link
Author

passy commented Jun 28, 2017

Turns out I just cannot read. The README describes exactly this behavior. mainBody is what I wanted.

Sorry, I'll get a coffee now in hopes of this improving my reading comprehension. ☕️

@passy passy closed this as completed Jun 28, 2017
@xenomachina
Copy link
Owner

If you have any ideas for how this could be made clearer in the docs, please let me know. Thanks!

@ThraaxSession
Copy link

Sorry, I've got same issue and can't figure out :D I didn't find anything in the README about this behavior. :(

@sifraser
Copy link

I didn't get it either.. but you need:
fun main(args: Array<String>) = mainBody { <your code> }

@ThraaxSession
Copy link

@sifraser thanks :D but I've got another problem then when I want to use coroutines...

fun main(args: Array<String>) = runBlocking<Unit>

any idea how to combine?

@xenomachina
Copy link
Owner

@GinoHereIam, you can wrap mainBody in runBlocking or vice-versa, depending on precise behavior you want. I haven't tried it, but I'm thinking it probably makes most sense to have mainBody on the outside, and to construct+force your parsed args object inside the mainBody before calling runBlocking. This will ensure that your arguments are parsed before starting your coroutines.

Something like this:

class ParsedArgs(parser: ArgParser) {
    val name by positional("The user's name").default("world")
}

fun main(args: Array<String>) = mainBody {
    val parser = ArgParser(args)
    val parsedArgs = ParsedArgs(parser)
    parser.force()

    runBlocking {
        <code that uses parsedArgs>
    }
}

@ThraaxSession
Copy link

sounds nice, I will try it when I am back at work. Thanks for you help. :)

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

4 participants