Skip to content

Conversation

@molisani
Copy link
Member

@molisani molisani commented Apr 1, 2025

Resolves #62

Describe your changes
Expose functionality for application developers to pull the default value for a flag from an environment variable. This was already possible manually, but integrating it as a first-class feature allows for better formatting of error messages and the ability to "redact" the value in Stricli-generated output (for secrets).

Testing performed
Added several new test cases and updated the formatting baselines to achieve complete coverage.

Additional context
One side-effect is that CommandContext["process"] is now StricliProcess rather than just WritableStreams to allow env to be defined. The differences between the two types are just optional properties so there shouldn't be any issues.

@molisani molisani added the enhancement ✨ New feature or request label Apr 1, 2025
@molisani molisani self-assigned this Apr 1, 2025
@molisani molisani requested a review from a team as a code owner April 1, 2025 16:48
@github-actions github-actions bot added core ⚙ Relates to the @stricli/core package documentation 📝 Improvements or additions to documentation labels Apr 1, 2025
// THEN
compareToBaseline(this, StringArrayBaselineFormat, lines);
});

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we also add a redact: true test?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah good catch, didn't have a case for redact in the formatting baselines.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh at the moment there isn't any difference in the help text when redact: true is enabled. Should there be?

    [--requiredBoolean/--noRequiredBoolean]  required boolean flag           [default = <from env REQUIRED_BOOLEAN>]
 -h --help                                   Print help information and exit

Copy link
Member Author

Choose a reason for hiding this comment

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

At the moment the help text doesn't display the value of the environment variable, only the name. While I think it would be incredibly handy to print the value (or redacted value) we also still need to print the name and all of that takes up a lot of space. Not sure what the best display would be yet.

Copy link
Member Author

@molisani molisani Apr 23, 2025

Choose a reason for hiding this comment

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

Which of these options would be the best for displaying the default value? Right now we have the last one, which has the added benefit of not needing to read from the process to build the help text.

    [--flag]  some flag           [default = ACTUAL_VALUE]
    [--flag]  some flag           [default = ACTUAL_VALUE <from env REQUIRED_BOOLEAN>]
    [--flag]  some flag           [default = <from env REQUIRED_BOOLEAN>]

With the following for redact: true

    [--flag]  some flag           [default = ************]
    [--flag]  some flag           [default = ************ <from env REQUIRED_BOOLEAN>]
    [--flag]  some flag           [default = <from env REQUIRED_BOOLEAN>]

Copy link
Collaborator

Choose a reason for hiding this comment

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

My preference would be option (2) followed by (1) if length is a concern.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good, we can do 2 and still have the backout to 3 if there is no value set.

@molisani molisani force-pushed the feat/env-flag branch 2 times, most recently from 3d56824 to 9640f8b Compare April 9, 2025 14:52
@molisani molisani requested a review from mkubilayk April 23, 2025 16:30
mkubilayk
mkubilayk previously approved these changes Apr 25, 2025
Copy link
Collaborator

@mkubilayk mkubilayk left a comment

Choose a reason for hiding this comment

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

My preference on formatting: #75 (comment).

@molisani
Copy link
Member Author

The output now looks like this depending on value/config:

  [--flag]  flag brief    [default = env:ENV_VAR_NAME]
  [--flag]  flag brief    [default = "" | env:ENV_VAR_NAME]
  [--flag]  flag brief    [default = SOME_VALUE | env:ENV_VAR_NAME]
  [--flag]  flag brief    [default = ██████████ | env:ENV_VAR_NAME]

I switched the redact character from * to █ so that it was even more obvious that the value is purposefully hidden.

@molisani molisani force-pushed the feat/env-flag branch 2 times, most recently from 3a7518d to 1c0caa8 Compare October 31, 2025 20:10
@molisani
Copy link
Member Author

molisani commented Nov 3, 2025

One problem with extending "default" to include environment variable values is that with normal defaults there is always a value but in the case where the variable is undefined then there is no value. Does it make sense to keep it as an extension of "default" or to split it off so that we can read from the env var and also have a default value?

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

Labels

core ⚙ Relates to the @stricli/core package documentation 📝 Improvements or additions to documentation enhancement ✨ New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

First-class support for using env variables as flag defaults

3 participants