Coder Social home page Coder Social logo

Comments (3)

xenomachina avatar xenomachina commented on May 24, 2024

Thanks for reporting this. I've never used KScript, but ensuring that Kotlin-argparser works well with it sounds like a great idea.

For now, would it be possible to replace this:

val parsedArgs = mainBody {
    ArgParser(args).parseInto(::Args)
}

println(parsedArgs.message)

with this?

mainBody {
    val parsedArgs = ArgParser(args).parseInto(::Args)
    println(parsedArgs.message)
}

Making this less verbose would be nice, but part of the reason the "mainBody" is separate from "parseInto" is to make testing easier. mainBody calls exitProcess and also writes directly to System.out/System.err, and none of that is very easy to test. So the idea is that mainBody has factored out a bunch of difficult to test bits. The body you pass to mainBody can do nothing but call a "testableMain" method, and your tests can test that same method.

I should probably at least make this more clear in kotlin-argparser-example.

from kotlin-argparser.

exaV avatar exaV commented on May 24, 2024

with this?
mainBody { val parsedArgs = ArgParser(args).parseInto(::Args) println(parsedArgs.message) }

I reported this issue because I am not willing to put the whole script into a lambda in the first place for the same reasons I do not want to have a main method. The example I provided is somewhat minimal so maybe it did not seem significant there. However in any meaningful script most of the code would be wrapped into the mainBody lambda and thus have one level of indentation. Additional indentation makes the script harder to read, especially because usually there will be at least one or two more levels of indentation (say an if or a for).

I was already quite pleased when I realised I could use

val parsedArgs = mainBody {
    ArgParser(args).parseInto(::Args)
}

which lets me get around having additional indentation for the rest of the script. However having the mainBody lambda around parsing just seems unnecessarily verbose. Also my co-workers who are not familiar with the library asked my what the mainBody was for, which also hints at something, especially considering that they perfectly understood the rest of arg-parsing.

Coincidentally the maintainer of Kscript also opened an issue relating to this #65 which indicated that I'm not the only one with this issue. I understand your reservations you have regarding testability, however as a user I would still much prefer not having to write mainBody to parse arguments.

from kotlin-argparser.

exaV avatar exaV commented on May 24, 2024

Would it help testability if you declared the function I need as an extension function on Argparser in its own file? You could then test this file on its own.

I'm thinking of this:

fun <T> ArgParser.parse(constructor: (ArgParser) -> T) = mainBody {
    this.parseInto(constructor)
}

Of course the name could be different

from kotlin-argparser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.