Coder Social home page Coder Social logo

More readable struct fields about docopt.rs HOT 14 OPEN

docopt avatar docopt commented on June 3, 2024
More readable struct fields

from docopt.rs.

Comments (14)

BurntSushi avatar BurntSushi commented on June 3, 2024

The problem with the first approach is you can end up with collisions, although I suppose an error could be generated in that case.

@alexcrichton What do you think? I don't have any terribly strong opinions. The current approach was just something That Worked. I'm not attached to it.

from docopt.rs.

alexcrichton avatar alexcrichton commented on June 3, 2024

I'd avoid the first for the same reasons, and foo.options.bar does seem a little better than foo.flag_bar (foo.flags.bar?). I also don't have too too strong a preference!

from docopt.rs.

keleshev avatar keleshev commented on June 3, 2024

I think that a single namespace is still a viable option, because when was the last time anyone had a --file option and a <file> argument?!

from docopt.rs.

evnm avatar evnm commented on June 3, 2024

Right, wouldn't collisions represent symbol reuse within a single pattern? That seems inadvisable to begin with. Since the first, singly-namespaced approach better matches what other docopt implementations provide, I'd vote for it over struct nesting.

from docopt.rs.

BurntSushi avatar BurntSushi commented on June 3, 2024

Since the first, singly-namespaced approach better matches what other docopt implementations provide

The reference docopt implementation is namespaced.

If it's truly inadvisable to have a flag and, say, a positional argument with the same name, then perhaps Docopt itself should rule it out. But as of now, it allows it, so I don't really see a reason to arbitrarily disallow it.

from docopt.rs.

keleshev avatar keleshev commented on June 3, 2024

If it's truly inadvisable to have a flag and, say, a positional argument with the same name, then perhaps Docopt itself should rule it out. But as of now, it allows it, so I don't really see a reason to arbitrarily disallow it.

I would totally disallow it. It just happens that some major languages have fist-class support for json-like dictionaries, so the problem never came up there.

For languages where it makes more sense to generate a custom data type (like struct in Rust and C), I think it's totally fine to raise error in case of a collision (like --file vs <file>).

It's just a matter if we decide on a single namespace, or on multiple nested namespaces (options, arguments, commands).

from docopt.rs.

evnm avatar evnm commented on June 3, 2024

Correct me if I'm wrong, but the ref impl generates a dictionary where the only nested data structure is the <args> list. Options and positional arguments are inserted into the dictionary as top-level key value pairs.

Collisions could still occur across separate patterns in the event that e.g. a positional argument's name is the same as a command. I'm of the opinion that avoiding such cases of conflated terms would also remove a potential source of confusion for users.

from docopt.rs.

BurntSushi avatar BurntSushi commented on June 3, 2024

@evnm What you say is true, but that doesn't mean the dictionary isn't namespaced. For example, all flags start with - or --. Likewise, currently, struct fields that are flags start with flag_.

Arguments are always keys of the form <arg> and ARG. A command, is, by definition, everything else. Therefore there is no overlap.

In the reference Docopt, you can have a flag, command and positional argument all have the same name:

Usage: program -a <a> a

And it works fine:

[andrew@Liger docopt] python examples/quick_example.py -a hi a
{'-a': True,
 '<a>': 'hi',
 'a': True}

In other words: namespaced.

(My understanding is that @halst does not like this? Even in the reference implementation? I don't see what native JSON dictionaries have to do with whether you have proper namespaces or not. I had assumed this namespacing was an explicit design decision, so I copied it.)

from docopt.rs.

keleshev avatar keleshev commented on June 3, 2024

@evnm in the reference implementation, each --option, <argument> and command is inserted as key in a json-like dictionary. The values of those keys could be different:

  • for --options it's boolaen
  • for --options with an argument, it's a string
  • for --options with an argument which could be repeated (thanks to ... operator), it's a list of values
  • for options which take no argument, but could be repeated (-v...), the value is a number (of repetitions)
  • for commands it's same as for options with no argument
  • for <arguments> it is either a single string, or a list of strings if argument could be repeated.

Since in the reference implementation keys are arbitrary strings, the cannot collide.

from docopt.rs.

keleshev avatar keleshev commented on June 3, 2024

@BurntSushi the ref. imp. is using full names (with characters like -<>), not to separate them from each other, but only for readability.

from docopt.rs.

evnm avatar evnm commented on June 3, 2024

Gotcha, thank you both. Excuse my ignorance. I'm not very familiar with docopt and came across this project when poking around for Rust CLI libs.

This makes more sense now. Insofar as my less-informed opinion matters, I think the args.{command,options,arguments} approach is a good compromise between adhering to the reference implementation and leveraging Rust structs.

from docopt.rs.

BurntSushi avatar BurntSushi commented on June 3, 2024

@evnm That is the way I'm leaning. Note that structs are a convenience for this library. You can still access values using a hashmap with keys like the reference Docopt implementation: https://github.com/docopt/docopt.rs/blob/master/examples/hashmap.rs

If we really think that having a usage like program -a <a> a is a bad idea, then it should be properly codified in the Docopt spec.

from docopt.rs.

evnm avatar evnm commented on June 3, 2024

Such collision avoidance might be worth posing to the docopt folks via a separate issue on docopt/docopt, but is outside of the scope of #7. As long as the hashmap-based API has parity with the Python lib, then I think docopt.rs should have fairly free reign over the sugary struct-based API.

from docopt.rs.

keleshev avatar keleshev commented on June 3, 2024

I think docopt.rs should have fairly free reign over the sugary struct-based API.

๐Ÿ‘

from docopt.rs.

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.