Comments (7)
I don't think there's any way to get a list of values out of an enum's Type
other than using dart:mirrors
, which isn't a good option for a cross-platform package like args
.
from args.
This is close to the top of the list of features I want to add to unscripted:
Patches welcome!
from args.
Hi Martin,
if you have a specific enum in mind, couldn't you get much of what you want
using the following (not tested, it just illustrates the idea):
List listToStringList(List xs) => xs.map((value) =>
value.toString()).toList();
addOption('cmd', abbr: 'c', allowed: listToStringList(Commands.values));
It's a workaround, but not a huge one..
On Mon, Apr 27, 2015 at 8:01 PM, Natalie Weizenbaum <
[email protected]> wrote:
I don't think there's any way to get a list of values out of an enum's
Type other than using dart:mirrors, which isn't a good option for a
cross-platform package like args.—
Reply to this email directly or view it on GitHub
#11 (comment).
Erik Ernst - Google Danmark ApS
Skt Petri Passage 5, 2 sal, 1165 København K, Denmark
CVR no. 28866984
from args.
That still doesn't address adding help text for each option. Of course you could have a separate Map<Commands, String>
which stores that, but that's not as DRY as just annotating the enum members. And of course enums ought to be able to have their own fields, but then it still wouldn't be generic, unless there were some HelpTextEnum
or similar interface that enums could implement.
from args.
With the proposed metaclasses for Dart (see
https://github.com/gbracha/metaclasses), the expression Commands
would
evaluate to an object of type Type which represents the class Commands, and
it would respond to values
just like the static method values
on any
enum class.
This means that we could get the values of a given enum class and transform
each value into a String in a generic manner (with no static knowledge of
the exact enum class, just relying on the given instance of Type
representing that enum class) without any use of dart:mirrors. An
accompanying argument of type Map<Object, String> or String helpMapper(Object enumValue)
could then provide the help strings. The
typing is loose (we can't say that the arguments to a helpMapper must be a
value of the given enum class), but it would work, and it would be generic.
If it is considered worthwhile, that generic code could then sit in the
implementation of addOption
, and it could take extra arguments similar to
the following:
addOption(... {... allowedEnum: Type, allowedEnumHelp: Map<Object,
String>});
Otherwise it wouldn't be hard to write a helper function that receives
these arguments and calls addOption after having transformed the enum type
to a List for allowed
, and the allowedEnumHelp
mapping into a
suitable argument for allowedHelp
.
In any case, this would be blocked until we have those metaclasses.
On Mon, Apr 27, 2015 at 9:06 PM, Sean Eagan [email protected]
wrote:
That still doesn't address adding help text for each option. Of course you
could have a separate Map<Commands, String> which stores that, but that's
not as DRY as just annotating the enum members. And of course enums ought
to be able to have their own fields, but then it still wouldn't be generic,
unless there were some HelpText or similar interface that enums could
implement.—
Reply to this email directly or view it on GitHub
#11 (comment).
Erik Ernst - Google Danmark ApS
Skt Petri Passage 5, 2 sal, 1165 København K, Denmark
CVR no. 28866984
from args.
I didn't know there could be a problem with dart:mirrors
but something like this is better than nothing:
addOption('cmd', abbr: 'c', allowed: Commands.values)
Which can be easily converted to allowed string values:
values.map((val) => val.toString().split('.')[1]);
Then converting string arguments back to enums could work similarly.
from args.
I don't think that's substantially simpler than manually converting values to strings, and since it isn't typable it would be a lot harder to comprehend.
from args.
Related Issues (20)
- Overhaul usage errors to not use exceptions
- Examples for CommandRunner aren't correct on pub page HOT 3
- Statically typed argument parser (possibly with code generator)? HOT 3
- Add a CommandRunner example HOT 2
- Why is `CommandRunner._usageWithoutDescription` private?
- Promote Command.argResults to be non-nullable HOT 1
- Make allowed options case insensitive
- How to add default command?
- Provide the function to print strings in nice columns
- Standards used in docopt. HOT 2
- [Feature request] Show command alias in usage HOT 3
- Command CommandRunner Platform.executableArguments HOT 3
- use of 'mandatory' options breaks handling of the help flag
- Feature request: parse complete command string to List<String> args HOT 3
- Analogue of '::' GNU getopt extension? HOT 1
- `--` needs to be escaped on Windows HOT 2
- Usage should be able to show additional info for trailing options (rest) HOT 1
- Setting the mandatory parameter of the addOption function to true has no effect. HOT 2
- `takesArguments` should change the usage text
- Incorrect message in `dart_site --help` HOT 1
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 args.