Code Monkey home page Code Monkey logo

Comments (7)

nex3 avatar nex3 commented on June 12, 2024

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.

seaneagan avatar seaneagan commented on June 12, 2024

This is close to the top of the list of features I want to add to unscripted:

seaneagan/unscripted#13

Patches welcome!

from args.

eernstg avatar eernstg commented on June 12, 2024

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.

seaneagan avatar seaneagan commented on June 12, 2024

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.

eernstg avatar eernstg commented on June 12, 2024

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.

martinsik avatar martinsik commented on June 12, 2024

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.

nex3 avatar nex3 commented on June 12, 2024

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)

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.