Comments (3)
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.
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.
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)
- Allow '.' as Separator in Options and Arguments HOT 2
- Hierarchical arguments
- Print help when catching SystemExitException HOT 1
- Is it possible to allow number parameters HOT 3
- Incorrect example main function in README.md HOT 1
- Readme example does not work HOT 3
- Exception in thread "main" com.xenomachina.argparser.ShowHelpException: Help was requested HOT 3
- InvalidArgumentName when argument name has trailing digit HOT 2
- Allow escaping strings HOT 6
- Make `mainBody` behavior the default when parsing args HOT 3
- Help section titles should be capitalized HOT 2
- Interest in better support for sub parsers HOT 2
- Don't be too strict with positional and positionalList names
- Influcence exit behaviour
- better handling of long parameter names in the default formatter
- Is this project dead? HOT 1
- Support options of the form "-<number>" (hyphen immediately followed by a number)
- JCenter end of life
- kotlin-argparser and native?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlin-argparser.