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

Allow override of configuration options via the CLI #8368

Closed
zanieb opened this issue Oct 30, 2023 · 2 comments · Fixed by #9599
Closed

Allow override of configuration options via the CLI #8368

zanieb opened this issue Oct 30, 2023 · 2 comments · Fixed by #9599
Assignees
Labels
cli Related to the command-line interface configuration Related to settings and configuration

Comments

@zanieb
Copy link
Member

zanieb commented Oct 30, 2023

Currently, Ruff includes many configuration options that are only available via a configuration file. These options should be available via the CLI as well so users can either:

  • Provide configuration options without defining a configuration file
  • Override configuration options per invocation of Ruff without modifying files

There have been previous requests for this, but I could not find them. If you know where they're at please share so we can have more historical context here.

Proposals

There are a few options for exposing this capability.

Environment variables

Add environment variable overrides for all settings. Environment variables would take precedence over all persistent configuration options but not CLI flags.

A repeatable CLI option with key value pairs

A CLI option for overriding settings e.g. --setting <NAME>=<VALUE>

Alternative names include --settings, --override-setting, --override-config, --set-config, --configure, --override, --option, --options

Ruff already uses --config <CONFIG> to provide a path to a configuration file.
Cargo uses --config for both overriding options and providing the path to a configuration file.

A CLI option with JSON support

A CLI option for overriding settings in bulk e.g. --settings {...}

This has been requested for programmatic use of Ruff previously e.g. you can use jq to pass a configuration file in bulk. However, this is not a very user-friendly approach for most use cases.

Dedicated CLI options

A dedicated CLI option generated for each setting e.g. --set-<name> VALUE.

We would probably omit enumerating these in the help menus and document the pattern instead. We could omit the set- prefix but then we need to worry about collisions between configuration options and existing CLI flags. Additionally, this option does not work well with the <section>.<name> syntax used in our configuration.

@zanieb zanieb added configuration Related to settings and configuration cli Related to the command-line interface labels Oct 30, 2023
@zanieb
Copy link
Member Author

zanieb commented Oct 30, 2023

For what it's worth, I think that --config NAME=VALUE matching Cargo's design is reasonable.

@T-256
Copy link
Contributor

T-256 commented Oct 30, 2023

Loved these proposals, here are examples and cons of each:

1. Environment variables

RUFF_FORMAT_LINE_LENGTH=120 ruff format .

cons:

  • All words capitalized and - replaced by _
  • Problem with <section>.<name> settings
  • Long, less user friendly because they may be varied by different shells/OSs.

2. A repeatable CLI option with key value pairs (e.g. --config)

ruff format . --config line-length=120

cons:

  • it's long and it may not preferable where e.g. --line-length already available to choose.

3. A CLI option with JSON support (e.g. --config)

ruff format . --config {"line-length":120}
ruff format . --config eyJsaW5lLWxlbmd0aCI6MTIwfQ==

cons:

  • it's not clear it should merge with already loaded settings or reset it.
  • it's not clear what should happen when using it multiple times.

4. Dedicated CLI options (without any prefixes)

ruff format . --line-length=120

cons:

  • Collisions with existing CLI flags
  • Problem with <section>.<name> settings

I think 1, 2 and 3 can be supported at same time.
4 can be preferable over 2 when its conditions are available (no . in settings name).

@AlexWaygood AlexWaygood self-assigned this Jan 19, 2024
AlexWaygood added a commit that referenced this issue Feb 9, 2024
…9599)

Fixes #8368
Fixes #9186

## Summary

Arbitrary TOML strings can be provided via the command-line to override
configuration options in `pyproject.toml` or `ruff.toml`. As an example:
to run over typeshed and respect typeshed's `pyproject.toml`, but
override a specific isort setting and enable an additional pep8-naming
setting:

```
cargo run -- check ../typeshed --no-cache --config ../typeshed/pyproject.toml --config "lint.isort.combine-as-imports=false" --config "lint.extend-select=['N801']"
```

---------

Co-authored-by: Micha Reiser <[email protected]>
Co-authored-by: Zanie Blue <[email protected]>
nkxxll pushed a commit to nkxxll/ruff that referenced this issue Mar 10, 2024
…stral-sh#9599)

Fixes astral-sh#8368
Fixes astral-sh#9186

## Summary

Arbitrary TOML strings can be provided via the command-line to override
configuration options in `pyproject.toml` or `ruff.toml`. As an example:
to run over typeshed and respect typeshed's `pyproject.toml`, but
override a specific isort setting and enable an additional pep8-naming
setting:

```
cargo run -- check ../typeshed --no-cache --config ../typeshed/pyproject.toml --config "lint.isort.combine-as-imports=false" --config "lint.extend-select=['N801']"
```

---------

Co-authored-by: Micha Reiser <[email protected]>
Co-authored-by: Zanie Blue <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Related to the command-line interface configuration Related to settings and configuration
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants