Skip to content

Commit 5a3a2f8

Browse files
committed
slither: implement shell completions with shtab
These can be generated by running e.g. `slither --print-completion zsh` and installed as usual. Closes #2055
1 parent 9fa8e8e commit 5a3a2f8

File tree

15 files changed

+66
-19
lines changed

15 files changed

+66
-19
lines changed

Diff for: setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"eth-abi>=4.0.0",
2222
"eth-typing>=3.0.0",
2323
"eth-utils>=2.1.0",
24+
"shtab>=1.6.5",
2425
],
2526
extras_require={
2627
"lint": [

Diff for: slither/__main__.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from crytic_compile.platform.standard import generate_standard_export
1919
from crytic_compile.platform.etherscan import SUPPORTED_NETWORK
2020
from crytic_compile import compile_all, is_supported
21+
import shtab
2122

2223
from slither.detectors import all_detectors
2324
from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification
@@ -290,6 +291,8 @@ def parse_args(
290291
usage=usage,
291292
)
292293

294+
shtab.add_argument_to(parser)
295+
293296
parser.add_argument("filename", help=argparse.SUPPRESS)
294297

295298
cryticparser.init(parser)
@@ -465,28 +468,28 @@ def parse_args(
465468
help='Export the results as a JSON file ("--json -" to export to stdout)',
466469
action="store",
467470
default=defaults_flag_in_config["json"],
468-
)
471+
).complete = shtab.FILE
469472

470473
group_misc.add_argument(
471474
"--sarif",
472475
help='Export the results as a SARIF JSON file ("--sarif -" to export to stdout)',
473476
action="store",
474477
default=defaults_flag_in_config["sarif"],
475-
)
478+
).complete = shtab.FILE
476479

477480
group_misc.add_argument(
478481
"--sarif-input",
479482
help="Sarif input (beta)",
480483
action="store",
481484
default=defaults_flag_in_config["sarif_input"],
482-
)
485+
).complete = shtab.FILE
483486

484487
group_misc.add_argument(
485488
"--sarif-triage",
486489
help="Sarif triage (beta)",
487490
action="store",
488491
default=defaults_flag_in_config["sarif_triage"],
489-
)
492+
).complete = shtab.FILE
490493

491494
group_misc.add_argument(
492495
"--json-types",
@@ -502,13 +505,14 @@ def parse_args(
502505
help="Export the results as a zipped JSON file",
503506
action="store",
504507
default=defaults_flag_in_config["zip"],
505-
)
508+
).complete = shtab.FILE
506509

507510
group_misc.add_argument(
508511
"--zip-type",
509512
help=f'Zip compression type. One of {",".join(ZIP_TYPES_ACCEPTED.keys())}. Default lzma',
510513
action="store",
511514
default=defaults_flag_in_config["zip_type"],
515+
choices=list(ZIP_TYPES_ACCEPTED.keys()),
512516
)
513517

514518
group_misc.add_argument(
@@ -540,7 +544,7 @@ def parse_args(
540544
action="store",
541545
dest="config_file",
542546
default=None,
543-
)
547+
).complete = shtab.FILE
544548

545549
group_misc.add_argument(
546550
"--change-line-prefix",

Diff for: slither/tools/doctor/__main__.py

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44

55
from crytic_compile import cryticparser
6+
import shtab
67

78
from slither.tools.doctor.utils import report_section
89
from slither.tools.doctor.checks import ALL_CHECKS
@@ -18,6 +19,8 @@ def parse_args() -> argparse.Namespace:
1819
usage="slither-doctor project",
1920
)
2021

22+
shtab.add_argument_to(parser)
23+
2124
parser.add_argument("project", help="The codebase to be tested.")
2225

2326
# Add default arguments from crytic-compile

Diff for: slither/tools/documentation/__main__.py

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import uuid
44
from typing import Optional, Dict, List
55
from crytic_compile import cryticparser
6+
import shtab
67
from slither import Slither
78
from slither.core.compilation_unit import SlitherCompilationUnit
89
from slither.core.declarations import Function
@@ -26,6 +27,8 @@ def parse_args() -> argparse.Namespace:
2627
usage="slither-documentation filename",
2728
)
2829

30+
shtab.add_argument_to(parser)
31+
2932
parser.add_argument("project", help="The target directory/Solidity file.")
3033

3134
parser.add_argument(

Diff for: slither/tools/erc_conformance/__main__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Any, Dict, List, Callable
55

66
from crytic_compile import cryticparser
7+
import shtab
78

89
from slither import Slither
910
from slither.core.declarations import Contract
@@ -42,6 +43,8 @@ def parse_args() -> argparse.Namespace:
4243
usage="slither-check-erc project contractName",
4344
)
4445

46+
shtab.add_argument_to(parser)
47+
4548
parser.add_argument("project", help="The codebase to be tested.")
4649

4750
parser.add_argument(
@@ -61,7 +64,7 @@ def parse_args() -> argparse.Namespace:
6164
help='Export the results as a JSON file ("--json -" to export to stdout)',
6265
action="store",
6366
default=False,
64-
)
67+
).complete = shtab.FILE
6568

6669
# Add default arguments from crytic-compile
6770
cryticparser.init(parser)

Diff for: slither/tools/flattening/__main__.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from crytic_compile import cryticparser
66
from crytic_compile.utils.zip import ZIP_TYPES_ACCEPTED
7+
import shtab
78

89
from slither import Slither
910
from slither.tools.flattening.flattening import (
@@ -28,6 +29,8 @@ def parse_args() -> argparse.Namespace:
2829
usage="slither-flat filename",
2930
)
3031

32+
shtab.add_argument_to(parser)
33+
3134
parser.add_argument("filename", help="The filename of the contract or project to analyze.")
3235

3336
parser.add_argument("--contract", help="Flatten one contract.", default=None)
@@ -44,27 +47,28 @@ def parse_args() -> argparse.Namespace:
4447
"--dir",
4548
help=f"Export directory (default: {DEFAULT_EXPORT_PATH}).",
4649
default=None,
47-
)
50+
).complete = shtab.DIRECTORY
4851

4952
group_export.add_argument(
5053
"--json",
5154
help='Export the results as a JSON file ("--json -" to export to stdout)',
5255
action="store",
5356
default=None,
54-
)
57+
).complete = shtab.FILE
5558

5659
parser.add_argument(
5760
"--zip",
5861
help="Export all the files to a zip file",
5962
action="store",
6063
default=None,
61-
)
64+
).complete = shtab.FILE
6265

6366
parser.add_argument(
6467
"--zip-type",
6568
help=f"Zip compression type. One of {','.join(ZIP_TYPES_ACCEPTED.keys())}. Default lzma",
6669
action="store",
6770
default=None,
71+
choices=list(ZIP_TYPES_ACCEPTED.keys()),
6872
)
6973

7074
group_patching = parser.add_argument_group("Patching options")

Diff for: slither/tools/interface/__main__.py

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44

55
from crytic_compile import cryticparser
6+
import shtab
67

78
from slither import Slither
89
from slither.utils.code_generation import generate_interface
@@ -22,6 +23,8 @@ def parse_args() -> argparse.Namespace:
2223
usage=("slither-interface <ContractName> <source file or deployment address>"),
2324
)
2425

26+
shtab.add_argument_to(parser)
27+
2528
parser.add_argument(
2629
"contract_source",
2730
help="The name of the contract (case sensitive) followed by the deployed contract address if verified on etherscan or project directory/filename for local contracts.",

Diff for: slither/tools/kspec_coverage/__main__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import argparse
44
from crytic_compile import cryticparser
5+
import shtab
56
from slither.tools.kspec_coverage.kspec_coverage import kspec_coverage
67

78
logging.basicConfig()
@@ -26,6 +27,8 @@ def parse_args() -> argparse.Namespace:
2627
usage="slither-kspec-coverage contract.sol kspec.md",
2728
)
2829

30+
shtab.add_argument_to(parser)
31+
2932
parser.add_argument(
3033
"contract", help="The filename of the contract or truffle directory to analyze."
3134
)
@@ -45,7 +48,7 @@ def parse_args() -> argparse.Namespace:
4548
help='Export the results as a JSON file ("--json -" to export to stdout)',
4649
action="store",
4750
default=False,
48-
)
51+
).complete = shtab.FILE
4952

5053
cryticparser.init(parser)
5154

Diff for: slither/tools/mutator/__main__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import shutil
77
from typing import Type, List, Any, Optional
88
from crytic_compile import cryticparser
9+
import shtab
910
from slither import Slither
1011
from slither.tools.mutator.mutators import all_mutators
1112
from slither.utils.colors import yellow, magenta
@@ -38,6 +39,8 @@ def parse_args() -> argparse.Namespace:
3839
usage="slither-mutate <codebase> --test-cmd <test command> <options>",
3940
)
4041

42+
shtab.add_argument_to(parser)
43+
4144
parser.add_argument("codebase", help="Codebase to analyze (.sol file, project directory, ...)")
4245

4346
parser.add_argument(
@@ -63,7 +66,7 @@ def parse_args() -> argparse.Namespace:
6366
# output directory argument
6467
parser.add_argument(
6568
"--output-dir", help="Name of output directory (by default 'mutation_campaign')"
66-
)
69+
).complete = shtab.DIRECTORY
6770

6871
# to print just all the mutants
6972
parser.add_argument(

Diff for: slither/tools/possible_paths/__main__.py

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from argparse import ArgumentParser, Namespace
55

66
from crytic_compile import cryticparser
7+
import shtab
78
from slither import Slither
89
from slither.core.declarations import FunctionContract
910
from slither.utils.colors import red
@@ -27,6 +28,8 @@ def parse_args() -> Namespace:
2728
usage="possible_paths.py filename [contract.function targets]",
2829
)
2930

31+
shtab.add_argument_to(parser)
32+
3033
parser.add_argument(
3134
"filename", help="The filename of the contract or truffle directory to analyze."
3235
)

Diff for: slither/tools/properties/__main__.py

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Any
55

66
from crytic_compile import cryticparser
7+
import shtab
78

89
from slither import Slither
910
from slither.tools.properties.properties.erc20 import generate_erc20, ERC20_PROPERTIES
@@ -73,6 +74,8 @@ def parse_args() -> argparse.Namespace:
7374
formatter_class=argparse.RawDescriptionHelpFormatter,
7475
)
7576

77+
shtab.add_argument_to(parser)
78+
7679
parser.add_argument(
7780
"filename", help="The filename of the contract or project directory to analyze."
7881
)

Diff for: slither/tools/read_storage/__main__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import argparse
66

77
from crytic_compile import cryticparser
8+
import shtab
89

910
from slither import Slither
1011
from slither.exceptions import SlitherError
@@ -29,6 +30,8 @@ def parse_args() -> argparse.Namespace:
2930
),
3031
)
3132

33+
shtab.add_argument_to(parser)
34+
3235
parser.add_argument(
3336
"contract_source",
3437
help="The deployed contract address if verified on etherscan. Prepend project directory for unverified contracts.",
@@ -77,7 +80,7 @@ def parse_args() -> argparse.Namespace:
7780
"--json",
7881
action="store",
7982
help="Save the result in a JSON file.",
80-
)
83+
).complete = shtab.FILE
8184

8285
parser.add_argument(
8386
"--value",

Diff for: slither/tools/similarity/__main__.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66

77
from crytic_compile import cryticparser
8+
import shtab
89

910
from slither.tools.similarity.info import info
1011
from slither.tools.similarity.test import test
@@ -22,11 +23,15 @@ def parse_args() -> argparse.Namespace:
2223
description="Code similarity detection tool. For usage, see https://github.com/crytic/slither/wiki/Code-Similarity-detector"
2324
)
2425

25-
parser.add_argument("mode", help="|".join(modes))
26+
shtab.add_argument_to(parser)
27+
28+
parser.add_argument("mode", help="|".join(modes), choices=modes)
2629

2730
parser.add_argument("model", help="model.bin")
2831

29-
parser.add_argument("--filename", action="store", dest="filename", help="contract.sol")
32+
parser.add_argument(
33+
"--filename", action="store", dest="filename", help="contract.sol"
34+
).complete = shtab.FILE
3035

3136
parser.add_argument("--fname", action="store", dest="fname", help="Target function")
3237

@@ -51,7 +56,7 @@ def parse_args() -> argparse.Namespace:
5156

5257
parser.add_argument(
5358
"--input", action="store", dest="input", help="File or directory used as input"
54-
)
59+
).complete = shtab.FILE
5560

5661
parser.add_argument(
5762
"--version",

Diff for: slither/tools/slither_format/__main__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import argparse
33
import logging
44
from crytic_compile import cryticparser
5+
import shtab
56
from slither import Slither
67
from slither.utils.command_line import read_config_file
78
from slither.tools.slither_format.slither_format import slither_format
@@ -30,6 +31,8 @@ def parse_args() -> argparse.Namespace:
3031
"""
3132
parser = argparse.ArgumentParser(description="slither_format", usage="slither_format filename")
3233

34+
shtab.add_argument_to(parser)
35+
3336
parser.add_argument(
3437
"filename", help="The filename of the contract or truffle directory to analyze."
3538
)
@@ -60,7 +63,7 @@ def parse_args() -> argparse.Namespace:
6063
action="store",
6164
dest="config_file",
6265
default="slither.config.json",
63-
)
66+
).complete = shtab.FILE
6467

6568
group_detector = parser.add_argument_group("Detectors")
6669
group_detector.add_argument(

0 commit comments

Comments
 (0)