Coder Social home page Coder Social logo

Comments (4)

ajalt avatar ajalt commented on September 18, 2024 1

Thanks for the example. There are lots of annotation based CLI libraries out there, and I touched on the disadvantages of them in the docs. They require reflection and code generation, they're less type safe, and are harder to customize than Clikt. But feel free to use one if you prefer; Clikt will never be an annotation based library.

It seems that the folks who have issues with Clikt's inheritance are trying to use a CliktCommand as their data model. Clikt is UI, and like all UI, coupling it to your data layer can make it harder to test. You probably wouldn't define your data model in HTML fields on DOM nodes, or on properties on an Android View.

It's easy to make a plain data class from a command:

fun MyCommand.toModel() = MyModel(
    foo=foo,
    bar=bar,
    //...
)

val model = MyCommand().apply{ main(argv) }.toModel()

from clikt.

ajalt avatar ajalt commented on September 18, 2024

Another solution is to use composition instead of inheritance.

What exactly would that solution look like?

from clikt.

aoli-al avatar aoli-al commented on September 18, 2024

Similar to this #453 (comment)

If I can create a data object

data class Configuration(val @Option("clazz", type=String...) clazz: String, val: report: String, val targetArgs: AlgoOption) { // if we can configure the options through argument annotations. 
    init(args: CliKt) { // If CliKt can generate this constructor automatically...
        init(args.clazz, args.report, args.algo)
    }
}

and construct the data class through clikt in my main program

val config: Configuration = parser.parse(args)

Then I can just construct my Configuration object in unit tests freely.

val config = Configuration("", ...)

from clikt.

aoli-al avatar aoli-al commented on September 18, 2024

I see, Thanks for your explanation. It makes sense to me.

from clikt.

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.