Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: improve cli module #78337

Merged
merged 7 commits into from Jan 24, 2020
Merged

Conversation

Profpatsch
Copy link
Member

Here are the changes I suggested in the original PR after it was merged a few days ago.

Since this module is so young, I think we can still make breaking changes to clean up the interface and make it conform more to existing functions. In particular, it’s extremely similar to how I structured generators.nix, so I tried making it conform more to that.

cc @Gabriel439 as original author.

# how to string-format the option name;
# by default one character is a short option (`-`),
# more than one characters a long option (`--`).
mkOptionName ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to avoid the last use of render here, but mk seems a bit odd. The mk prefix is normally used for functions that assemble attrsets or derivations. These functions deconstruct rather than construct, so I'd go with from.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole exercise is to make it have the same interface that is used in generators.nix, which uses mk for conversion attributes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your good intent, but that module also seems to use mk for no apparent reason. It is not making instances of the term it prefixes. It is consuming them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it might not have been the best naming, but I’m arguing for consistency. We can’t change the generators.nix interface anymore, so we need to change this if we want consistency.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And internal consistency > perfect naming, always.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear how consistency necessitates choosing a misleading name. You could always add new utilities to generators.nix with a better naming convention later and deprecate the misleading naming convention without removing it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, but for now let’s keep it that way.

lib/default.nix Outdated Show resolved Hide resolved
lib/cli is very similar to generators, so it should follow largely the
same interface. Similar to how generators isn’t exported, we should
also namespace cli by default (plus “cli” is only three characters to
type).
The semantic difference between `encode` and `to` is not apparent.
Users are likely to confuse both functions (which leads to unexpected
error messages about the wrong types). Like in `generators.nix`, all
functions should be prefixed by `to`.

Furthermore, converting to a string depends on the target context. In
this case, it’s a POSIX shell, so we should name it that (compare
`escapeShellArg` in `strings.nix`).

We can later add versions that escape for embedding in e.g. python
scripts or similar.
Mirrors the naming scheme in `generators.nix`, for consistency.

Also rename `key` to `k` and value to `v` to aid readability to the
code structure.
They are cut off after a few decimal places; we cannot in good faith
define a default string representation with that.
Let’s call them by what they are, option names.

`generators.mkValueStringDefault` is a better value string renderer
than plain `toString`.

Also add docs to all options.
@Profpatsch
Copy link
Member Author

I’ll merge, if we decide we want to rename and deprecate later, we can do that.

@Profpatsch Profpatsch merged commit 07eb21c into NixOS:master Jan 24, 2020
dtzWill pushed a commit to dtzWill/nixpkgs that referenced this pull request Jan 24, 2020
lib: improve cli module
(cherry picked from commit 07eb21c)
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/january-2020-in-nixos/5771/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants