Skip to content

Commit 32c8dc5

Browse files
authored
Merge pull request #1229 from volatilityfoundation/feature/argcomplete
Feature/argcomplete
2 parents eb757b4 + 0bda154 commit 32c8dc5

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

vol.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python3
2+
# PYTHON_ARGCOMPLETE_OK
23

34
# This file is Copyright 2019 Volatility Foundation and licensed under the Volatility Software License 1.0
45
# which is available at https://www.volatilityfoundation.org/license/vsl-v1.0

volatility3/cli/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
from typing import Any, Dict, List, Tuple, Type, Union
2323
from urllib import parse, request
2424

25+
try:
26+
import argcomplete
27+
28+
HAS_ARGCOMPLETE = True
29+
except ImportError:
30+
HAS_ARGCOMPLETE = False
31+
2532
from volatility3.cli import text_filter
2633
import volatility3.plugins
2734
import volatility3.symbols
@@ -351,6 +358,10 @@ def run(self):
351358
# Hand the plugin requirements over to the CLI (us) and let it construct the config tree
352359

353360
# Run the argparser
361+
if HAS_ARGCOMPLETE:
362+
# The autocompletion line must be after the partial_arg handling, so that it doesn't trip it
363+
# before all the plugins have been added
364+
argcomplete.autocomplete(parser)
354365
args = parser.parse_args()
355366
if args.plugin is None:
356367
parser.error("Please select a plugin to run")

volatility3/cli/volargparse.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ class HelpfulSubparserAction(argparse._SubParsersAction):
2121

2222
def __init__(self, *args, **kwargs) -> None:
2323
super().__init__(*args, **kwargs)
24-
# We don't want the action self-check to kick in, so we remove the choices list, the check happens in __call__
25-
self.choices = None
2624

2725
def __call__(
2826
self,
@@ -100,3 +98,20 @@ def _match_argument(self, action, arg_strings_pattern) -> int:
10098

10199
# return the number of arguments matched
102100
return len(match.group(1))
101+
102+
def _check_value(self, action: argparse.Action, value: Any) -> None:
103+
"""This is called to ensure a value is correct/valid
104+
105+
In normal operation, it would check that a value provided is valid and return None
106+
If it was not valid, it would throw an ArgumentError
107+
108+
When people provide a partial plugin name, we want to look for a matching plugin name
109+
which happens in the HelpfulSubparserAction's __call_method
110+
111+
To get there without tripping the check_value failure, we have to prevent the exception
112+
being thrown when the value is a HelpfulSubparserAction. This therefore affects no other
113+
checks for normal parameters.
114+
"""
115+
if not isinstance(action, HelpfulSubparserAction):
116+
super()._check_value(action, value)
117+
return None

volatility3/cli/volshell/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
plugins,
2222
)
2323

24+
try:
25+
import argcomplete
26+
27+
HAS_ARGCOMPLETE = True
28+
except ImportError:
29+
HAS_ARGCOMPLETE = False
30+
31+
2432
# Make sure we log everything
2533

2634
rootlog = logging.getLogger()
@@ -276,6 +284,10 @@ def run(self):
276284
# Hand the plugin requirements over to the CLI (us) and let it construct the config tree
277285

278286
# Run the argparser
287+
if HAS_ARGCOMPLETE:
288+
# The autocompletion line must be after the partial_arg handling, so that it doesn't trip it
289+
# before all the plugins have been added
290+
argcomplete.autocomplete(parser)
279291
args = parser.parse_args()
280292

281293
vollog.log(

volshell.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python3
2+
# PYTHON_ARGCOMPLETE_OK
23

34
# This file is Copyright 2019 Volatility Foundation and licensed under the Volatility Software License 1.0
45
# which is available at https://www.volatilityfoundation.org/license/vsl-v1.0

0 commit comments

Comments
 (0)