Skip to content

Commit 5aa10e1

Browse files
committed
Added basic 'argcomplete' completion
completes argument names, sub_parser names and available cores/tools/generators argcomplete completion can be enabled by two ways: 1. Global: can be enabled with the 'activate-global-python-argcomplete' command additionaly the fusesoc binary needs the string PYTHON_ARGCOMPLETE_OK in the first 1024 bytes, this needs to be added to the pip binary to work this only works for zsh and bash 2: Per command: for zsh and bash: eval "$(register-python-argcomplete fusesoc)" for other shells (e.g. powershell): https://github.com/kislyuk/argcomplete/tree/develop/contrib
1 parent 23c0592 commit 5aa10e1

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

fusesoc/main.py

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
# PYTHON_ARGCOMPLETE_OK
23
# Copyright FuseSoC contributors
34
# Licensed under the 2-Clause BSD License, see LICENSE for details.
45
# SPDX-License-Identifier: BSD-2-Clause
@@ -11,6 +12,8 @@
1112
import warnings
1213
from pathlib import Path
1314

15+
import argcomplete
16+
1417
try:
1518
from fusesoc.version import version as __version__
1619
except ImportError:
@@ -393,6 +396,41 @@ def update(fs, args):
393396
fs.update_libraries(args.libraries)
394397

395398

399+
class CoreCompleter:
400+
def __call__(self, parsed_args, **kwargs):
401+
config = Config(parsed_args.config)
402+
args_to_config(parsed_args, config)
403+
fs = Fusesoc(config)
404+
cores = fs.get_cores()
405+
return cores
406+
407+
408+
class ToolCompleter:
409+
def __call__(self, parsed_args, **kwargs):
410+
from edalize.edatool import get_edatool, walk_tool_packages
411+
412+
_tp = list(walk_tool_packages())
413+
tools = []
414+
for tool_name in _tp:
415+
try:
416+
tool_class = get_edatool(tool_name)
417+
if tool_class.get_doc(0)["description"]:
418+
tools += [tool_name]
419+
# Ignore any misbehaving backends
420+
except Exception:
421+
pass
422+
return tools
423+
424+
425+
class GenCompleter:
426+
def __call__(self, parsed_args, **kwargs):
427+
config = Config(parsed_args.config)
428+
args_to_config(parsed_args, config)
429+
fs = Fusesoc(config)
430+
cores = fs.get_generators()
431+
return cores
432+
433+
396434
def get_parser():
397435
parser = argparse.ArgumentParser()
398436
subparsers = parser.add_subparsers()
@@ -444,7 +482,9 @@ def get_parser():
444482
parser_core_show = core_subparsers.add_parser(
445483
"show", help="Show information about a core"
446484
)
447-
parser_core_show.add_argument("core", help="Name of the core to show")
485+
parser_core_show.add_argument(
486+
"core", help="Name of the core to show"
487+
).completer = CoreCompleter()
448488
parser_core_show.set_defaults(func=core_info)
449489

450490
# tool subparser
@@ -466,7 +506,7 @@ def get_parser():
466506
parser_core_info = subparsers.add_parser(
467507
"core-info", help="Display details about a core"
468508
)
469-
parser_core_info.add_argument("core")
509+
parser_core_info.add_argument("core").completer = CoreCompleter()
470510
parser_core_info.set_defaults(func=core_info)
471511

472512
# gen subparser
@@ -486,7 +526,9 @@ def get_parser():
486526
parser_gen_show = gen_subparsers.add_parser(
487527
"show", help="Show information about a generator"
488528
)
489-
parser_gen_show.add_argument("generator", help="Name of the generator to show")
529+
parser_gen_show.add_argument(
530+
"generator", help="Name of the generator to show"
531+
).completer = GenCompleter()
490532
parser_gen_show.set_defaults(func=gen_show)
491533

492534
# gen clean subparser
@@ -581,7 +623,9 @@ def get_parser():
581623
parser_run.add_argument("--build", action="store_true", help="Execute build stage")
582624
parser_run.add_argument("--run", action="store_true", help="Execute run stage")
583625
parser_run.add_argument("--target", help="Override default target")
584-
parser_run.add_argument("--tool", help="Override default tool for target")
626+
parser_run.add_argument(
627+
"--tool", help="Override default tool for target"
628+
).completer = ToolCompleter()
585629
parser_run.add_argument(
586630
"--flag",
587631
help="Set custom use flags. Can be specified multiple times",
@@ -601,7 +645,9 @@ def get_parser():
601645
action="store_true",
602646
help="Allow additional properties in core files",
603647
)
604-
parser_run.add_argument("system", help="Select a system to operate on")
648+
parser_run.add_argument(
649+
"system", help="Select a system to operate on"
650+
).completer = CoreCompleter()
605651
parser_run.add_argument(
606652
"backendargs", nargs=argparse.REMAINDER, help="arguments to be sent to backend"
607653
)
@@ -624,6 +670,7 @@ def get_parser():
624670
def parse_args(argv):
625671
parser = get_parser()
626672

673+
argcomplete.autocomplete(parser, always_complete_options=False)
627674
args = parser.parse_args(argv)
628675

629676
if hasattr(args, "func"):

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def read(fname):
5757
"simplesat>=0.8.0",
5858
"fastjsonschema",
5959
"jsonschema2md",
60+
"argcomplete",
6061
],
6162
# Supported Python versions: 3.6+
6263
python_requires=">=3.6, <4",

0 commit comments

Comments
 (0)