Repository: iterative/shtab

Issue:
[feature] why shtab don't have shell completion by itself?
I notice shtab.main.get_main_parser don't use `shtab.add_argument_to`. Can it do?

Discussion:
sourcery-ai[bot]: # Sourcery Code Quality Report

✅ &nbsp;Merging this PR will **increase** code quality in the affected files by **0.12%**.

| Quality metrics | Before | After | Change |
| --- | :---: | :---: | :---: |
| [Complexity](https://docs.sourcery.ai/GitHub/Quality-Report/#complexity) | 5.00 ⭐ | 4.85 ⭐ | -0.15 👍 |
| [Method Length](https://docs.sourcery.ai/GitHub/Quality-Report/#method-length) | 103.33 🙂 | 105.33 🙂 | 2.00 👎 |
| [Working memory](https://docs.sourcery.ai/GitHub/Quality-Report/#working-memory) | 5.50 ⭐ | 5.45 ⭐ | -0.05 👍 |
| **[Quality](https://docs.sourcery.ai/GitHub/Quality-Report/#quality-score)** | **70.21%** 🙂| **70.33%** 🙂| **0.12%** 👍 |

| Other metrics | Before | After | Change |
| --- | :---: | :---: | :---: |
| Lines | 54 | 55 | 1 |

| Changed files | Quality Before | Quality After | Quality Change |
| --- | :---: | :---: | :---: |
| shtab/main.py | 70.21% 🙂 | 70.33% 🙂 | 0.12% 👍|

Here are some functions in these files that still need a tune-up:

| File | Function | Complexity | Length | Working Memory | Quality | Recommendation |
| --- | --- | :---: | :---: | :---: | :---: | --- |
| shtab/main.py | main | 8 ⭐ | 148 😞 | 7 🙂 | 61.90% 🙂 | Try splitting into smaller methods |
| shtab/main.py | get\_main\_parser | 0 ⭐ | 126 😞 | 3 ⭐ | 82.58% ⭐ | Try splitting into smaller methods |

## Legend and Explanation

The emojis denote the absolute quality of the code:

 - ⭐ excellent
 - 🙂 good
 - 😞 poor
 - ⛔ very poor


The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.

***

Please see our documentation [here](https://docs.sourcery.ai/GitHub/Quality-Report/) for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us [improve](https://research.typeform.com/to/j06Spdfr?type=quality_report&github_login=Freed-Wu&base_repo=https%3A%2F%2Fgithub.com%2Fiterative%2Fshtab.git&base_remote_ref=refs%2Fpull%2F114%2Fhead&base_ref=master&base_sha=c84ef655bd16199eadf605ac777b30acd7929ca8&head_repo=https%3A%2F%2Fgithub.com%2Fiterative%2Fshtab.git&head_ref=sourcery%2Fpull-114&base_pr_number=114&base_pr_state=open) this quality report!
Freed-Wu: Any other problem?

Patch:
diff --git a/shtab/main.py b/shtab/main.py
index 36cd68c..bec291d 100644
--- a/shtab/main.py
+++ b/shtab/main.py
@@ -4,7 +4,7 @@ import os
 import sys
 from importlib import import_module
 
-from . import SUPPORTED_SHELLS, __version__, complete
+from . import SUPPORTED_SHELLS, __version__, add_argument_to, complete
 
 log = logging.getLogger(__name__)
 
@@ -26,6 +26,7 @@ def get_main_parser():
     )
     parser.add_argument("--verbose", dest="loglevel", action="store_const", default=logging.INFO,
                         const=logging.DEBUG, help="Log debug information")
+    add_argument_to(parser, "--print-own-completion", help="print shtab's own completion")
     return parser
 
 


Test patch:
diff --git a/tests/test_shtab.py b/tests/test_shtab.py
index f0f1f11..e07cede 100644
--- a/tests/test_shtab.py
+++ b/tests/test_shtab.py
@@ -64,6 +64,24 @@ def test_main(shell, caplog):
     assert not caplog.record_tuples
 
 
+@fix_shell
+def test_main_self_completion(shell, caplog, capsys):
+    with caplog.at_level(logging.INFO):
+        try:
+            main(["--print-own-completion", shell])
+        except SystemExit:
+            pass
+
+    captured = capsys.readouterr()
+    assert not captured.err
+    expected = {
+        "bash": "complete -o filenames -F _shtab_shtab shtab", "zsh": "_shtab_shtab_commands()",
+        "tcsh": "complete shtab"}
+    assert expected[shell] in captured.out
+
+    assert not caplog.record_tuples
+
+
 @fix_shell
 def test_prog_override(shell, caplog, capsys):
     with caplog.at_level(logging.INFO):