Repository: rochacbruno/dynaconf
Pull request #629 (2021-07-26 08:43:49)

Add a warning if `--env` is passed to `init`
Updated the CLI Click help strings, added in a warning print if `--env/-e` is passed to `init` to mention that it is a deprecated option an has no effect, also added a condition to raise an exception if `dynaconf -i ... init` is called as using instance with the init subcommand doesn't work and the standard exception may not be too clear.

Also updated `docs/cli.md` to be in sync with new help output from the subcommands, and to reflect the deprecation of `dynaconf init --env ...`.

Closes https://github.com/rochacbruno/dynaconf/issues/589

codecov[bot]: # [Codecov](https://codecov.io/gh/rochacbruno/dynaconf/pull/629?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bruno+Rocha) Report
> Merging [#629](https://codecov.io/gh/rochacbruno/dynaconf/pull/629?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bruno+Rocha) (42091a1) into [master](https://codecov.io/gh/rochacbruno/dynaconf/commit/90e7b464de297fde6b49c0306aa684d68fd7a0dc?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bruno+Rocha) (90e7b46) will **decrease** coverage by `0.12%`.
> The diff coverage is `50.00%`.

[![Impacted file tree graph](https://codecov.io/gh/rochacbruno/dynaconf/pull/629/graphs/tree.svg?width=650&height=150&src=pr&token=GXszVzJcqW&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bruno+Rocha)](https://codecov.io/gh/rochacbruno/dynaconf/pull/629?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bruno+Rocha)

```diff
@@             Coverage Diff             @@
##            master     #629      +/-   ##
===========================================
- Coverage   100.00%   99.87%   -0.13%     
===========================================
  Files           20       20              
  Lines         1553     1557       +4     
===========================================
+ Hits          1553     1555       +2     
- Misses           0        2       +2     
```


| [Impacted Files](https://codecov.io/gh/rochacbruno/dynaconf/pull/629?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bruno+Rocha) | Coverage Δ | |
|---|---|---|
| [dynaconf/cli.py](https://codecov.io/gh/rochacbruno/dynaconf/pull/629/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bruno+Rocha#diff-ZHluYWNvbmYvY2xpLnB5) | `99.12% <50.00%> (-0.88%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/rochacbruno/dynaconf/pull/629?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bruno+Rocha).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bruno+Rocha)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/rochacbruno/dynaconf/pull/629?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bruno+Rocha). Last update [90e7b46...42091a1](https://codecov.io/gh/rochacbruno/dynaconf/pull/629?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bruno+Rocha). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Bruno+Rocha).


Patch:
diff --git a/docs/cli.md b/docs/cli.md
index 49f5080..7f44335 100644
--- a/docs/cli.md
+++ b/docs/cli.md
@@ -18,28 +18,30 @@ Usage: dynaconf [OPTIONS] COMMAND [ARGS]...
 
   Dynaconf - Command Line Interface
 
+  Documentation: https://dynaconf.com/
+
 Options:
-  --version  Show dynaconf version
-  --docs     Open documentation in browser
-  --banner   Show awesome banner
+  --version            Show dynaconf version
+  --docs               Open documentation in browser
+  --banner             Show awesome banner
   -i, --instance TEXT  Custom instance of LazySettings
-  --help     Show this message and exit.
+  --help               Show this message and exit.
 
 Commands:
-  init      Inits a dynaconf project By default it...
-  list      Lists all defined config values
+  init      Inits a dynaconf project By default it creates a settings.toml...
+  list      Lists all user defined config values and if `--all` is passed
+            it...
+
+  validate  Validates Dynaconf settings based on rules defined in...
   write     Writes data to specific source
-  validate  Validates based on dynaconf_validators.toml file
 ```
 
 ### dynaconf init
 
 Use init to easily configure your application configuration, once dynaconf is installed go to the root directory of your application and run:
 
-creates settings files in current directory
-
 ```
-$ dynaconf -i init -v key=value -v foo=bar -s token=1234 -e production
+$ dynaconf init -v key=value -v foo=bar -s token=1234
 ```
 
 The above command will create in the current directory
@@ -47,11 +49,6 @@ The above command will create in the current directory
 `settings.toml`
 
 ```ini
-[default]
-KEY = "default"
-FOO = "default"
-
-[production]
 KEY = "value"
 FOO = "bar"
 ```
@@ -59,20 +56,10 @@ FOO = "bar"
 also `.secrets.toml`
 
 ```ini
-[default]
-TOKEN = "default"
-
-[production]
 TOKEN = "1234"
 ```
 
-The command will also create a `.env` setting the working environment to **[production]**
-
-```bash
-ENV_FOR_DYNACONF="PRODUCTION"
-```
-
-And will include the `.secrets.toml` in the `.gitignore`
+as well as `.gitignore` file ignoring the generated `.secrets.toml`
 
 ```ini
 # Ignore dynaconf secret files
@@ -93,25 +80,26 @@ Usage: dynaconf init [OPTIONS]
   This command must run on the project's root folder or you must pass
   --path=/myproject/root/folder.
 
-  If you want to have a .env created with the ENV defined there e.g:
-  `ENV_FOR_DYNACONF=production` just pass --env=production and then .env
-  will also be created and the env defined to production.
+  The --env/-e is deprecated (kept for compatibility but unused)
 
 Options:
   -f, --format [ini|toml|yaml|json|py|env]
   -p, --path TEXT                 defaults to current directory
   -e, --env TEXT                  Sets the working env in `.env` file
-  -v, --vars TEXT                 extra values to write to settings file file
-                                  e.g: `dynaconf init -v NAME=foo -v X=2
+  -v, --vars TEXT                 extra values to write to settings file e.g:
+                                  `dynaconf init -v NAME=foo -v X=2
+
   -s, --secrets TEXT              secret key values to be written in .secrets
                                   e.g: `dynaconf init -s TOKEN=kdslmflds
+
   --wg / --no-wg
   -y
   --django TEXT
   --help                          Show this message and exit.
-
 ```
 
+Note that `-i`/`--instance` cannot be used with `init` as `-i` must point to an existing instance of the settings.
+
 ### dynaconf list
 
 List all defined parameters and optionally export to a json file.
@@ -150,19 +138,22 @@ dynaconf list -o path/to/file.py --output-flat
 ### dynaconf write
 
 ```
-Usage: dynaconf write [OPTIONS] TO
+Usage: dynaconf write [OPTIONS] [ini|toml|yaml|json|py|redis|vault|env]
 
   Writes data to specific source
 
 Options:
-  -v, --vars TEXT     key values to be written e.g: `dynaconf write toml
-                      -e NAME=foo -e X=2
+  -v, --vars TEXT     key values to be written e.g: `dynaconf write toml -e
+                      NAME=foo -e X=2
+
   -s, --secrets TEXT  secret key values to be written in .secrets e.g:
                       `dynaconf write toml -s TOKEN=kdslmflds -s X=2
+
   -p, --path TEXT     defaults to current directory/settings.{ext}
   -e, --env TEXT      env to write to defaults to DEVELOPMENT for files for
                       external sources like Redis and Vault it will be
                       DYNACONF or the value set in $ENVVAR_PREFIX_FOR_DYNACONF
+
   -y
   --help              Show this message and exit.
 ```
diff --git a/dynaconf/cli.py b/dynaconf/cli.py
index 2d45e52..5bb8316 100644
--- a/dynaconf/cli.py
+++ b/dynaconf/cli.py
@@ -39,6 +39,10 @@ def set_settings(ctx, instance=None):
     settings = None
 
     if instance is not None:
+        if ctx.invoked_subcommand in ["init"]:
+            raise click.UsageError(
+                "-i/--instance option is not allowed for `init` command"
+            )
         sys.path.insert(0, ".")
         settings = import_settings(instance)
     elif "FLASK_APP" in os.environ:  # pragma: no cover
@@ -206,7 +210,10 @@ def main(ctx, instance):
     "--path", "-p", default=CWD, help="defaults to current directory"
 )
 @click.option(
-    "--env", "-e", default=None, help="Sets the working env in `.env` file"
+    "--env",
+    "-e",
+    default=None,
+    help="deprecated command (kept for compatibility but unused)",
 )
 @click.option(
     "--vars",
@@ -216,7 +223,7 @@ def main(ctx, instance):
     default=None,
     help=(
         "extra values to write to settings file "
-        "file e.g: `dynaconf init -v NAME=foo -v X=2"
+        "e.g: `dynaconf init -v NAME=foo -v X=2`"
     ),
 )
 @click.option(
@@ -251,6 +258,15 @@ def init(ctx, fileformat, path, env, _vars, _secrets, wg, y, django):
     click.echo("-" * 42)
     path = Path(path)
 
+    if env is not None:
+        click.secho(
+            "⚠️ The --env/-e option is deprecated (kept for\n"
+            "   compatibility but unused)\n",
+            fg="red",
+            bold=True,
+            # stderr=True,
+        )
+
     if settings.get("create_new_settings") is True:
         filename = Path("config.py")
         if not filename.exists():


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 9338851..6693701 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -35,6 +35,25 @@ def test_banner(clean_env):
     assert "Learn more at:" in run(["--banner"])
 
 
+def test_init_with_instance_raises(tmpdir):
+    result = run(
+        [
+            "-i",
+            "tests.test_cli.settings",
+            "init",
+            "--env",
+            "test",
+            f"--path={str(tmpdir)}",
+        ]
+    )
+    assert "-i/--instance option is not allowed for `init` command" in result
+
+
+def test_init_with_env_warns(tmpdir):
+    result = run(["init", "--env", "test", f"--path={str(tmpdir)}"])
+    assert "The --env/-e option is deprecated" in result
+
+
 @pytest.mark.parametrize("fileformat", EXTS)
 def test_init_with_path(fileformat, tmpdir):
     # run twice to force load of existing files