Comments (3)
Right now we only know how to parse from a string into value, but not go from an arbitrary value back into a string. We could use toString()
, but this would often produce output inconsistent with the transform function.
Straw man idea: default() could simply have an optional String parameter for how to represent the default value. If unset we use toString()
.
from kotlin-argparser.
default() could simply have an optional String parameter for how to represent the default value. If unset we use toString()
Sounds good to me
from kotlin-argparser.
I implemented this fairly naively...and added a bogus flag to HelpTest
:
class Args(parser: ArgParser) {
val dryRun by parser.flagging("-n", "--dry-run",
help = "don't do anything")
val includes by parser.adding("-I", "--include",
help = "search in this directory for header files")
val outDir by parser.storing("-o", "--output",
help = "directory in which all output should be generated")
val language by parser.storing("-l", "--language",
help = "language to output help message in")
.default("Latin")
val verbosity by parser.counting("-v", "--verbose",
help = "increase verbosity")
val sources by parser.positionalList("SOURCE",
help = "source file")
val destination by parser.positional("DEST",
help = "destination file")
}
The output looks like this:
usage: program_name [-h] [-n] [-I INCLUDE]... -o OUTPUT
[-l LANGUAGE] [-v]... SOURCE... DEST
This is the prologue. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Aliquam malesuada maximus eros.
Fusce luctus risus eget quam consectetur, eu auctor est
ullamcorper. Maecenas eget suscipit dui, sed sodales erat.
Phasellus.
This is the second paragraph of the prologue. I don't have
anything else to say, but I'd like there to be enough text
that it wraps to the next line.
required arguments:
-o OUTPUT, directory in which all output
--output OUTPUT should be generated
optional arguments:
-h, --help show this help message and exit
(default: kotlin.Unit)
-n, --dry-run don't do anything (default: false)
-I INCLUDE, search in this directory for
--include INCLUDE header files (default: [])
-l LANGUAGE, language to output help message in
--language LANGUAGE (default: Latin)
-v, --verbose increase verbosity (default: 0)
positional arguments:
SOURCE source file
DEST destination file
This is the epilogue. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Donec vel tortor nunc. Sed eu
massa sed turpis auctor faucibus. Donec vel pellentesque
tortor. Ut ultrices tempus lectus fermentum vestibulum.
Phasellus.
I'm not a fan of all the semi-confusing default values ((default: Kotlin.Unit)
, etc). So I added an opt-in boolean:
class Args(parser: ArgParser) {
val dryRun by parser.flagging("-n", "--dry-run",
help = "don't do anything")
val includes by parser.adding("-I", "--include",
help = "search in this directory for header files")
val outDir by parser.storing("-o", "--output",
help = "directory in which all output should be generated")
val language by parser.storing("-l", "--language",
help = "language to output help message in")
.default("Latin", show = true)
val verbosity by parser.counting("-v", "--verbose",
help = "increase verbosity")
val sources by parser.positionalList("SOURCE",
help = "source file")
val destination by parser.positional("DEST",
help = "destination file")
}
Which makes the output look like this:
usage: program_name [-h] [-n] [-I INCLUDE]... -o OUTPUT
[-l LANGUAGE] [-v]... SOURCE... DEST
This is the prologue. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Aliquam malesuada maximus eros.
Fusce luctus risus eget quam consectetur, eu auctor est
ullamcorper. Maecenas eget suscipit dui, sed sodales erat.
Phasellus.
This is the second paragraph of the prologue. I don't have
anything else to say, but I'd like there to be enough text
that it wraps to the next line.
required arguments:
-o OUTPUT, directory in which all output
--output OUTPUT should be generated
optional arguments:
-h, --help show this help message and exit
-n, --dry-run don't do anything
-I INCLUDE, search in this directory for
--include INCLUDE header files
-l LANGUAGE, language to output help message in
--language LANGUAGE (default: Latin)
-v, --verbose increase verbosity
positional arguments:
SOURCE source file
DEST destination file
This is the epilogue. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Donec vel tortor nunc. Sed eu
massa sed turpis auctor faucibus. Donec vel pellentesque
tortor. Ut ultrices tempus lectus fermentum vestibulum.
Phasellus.
This is all ultimately backed by fun <T> ArgParser.Delegate<T>.default(defaultValue: () -> T, showDefault: ((T) -> String)?): ArgParser.Delegate<T>
, which allows a user to use a non-toString
output if they're so inclined.
Let me know if this approach is makes sense or if there's some other ideas for formatting you had in mind.
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
- Include exception handling in force parse HOT 3
- 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.