[diffoscope] 01/02: Add argument completion feature to diffoscope

Reiner Herrmann reiner at reiner-h.de
Mon Jun 20 23:44:28 CEST 2016


This is an automated email from the git hooks/post-receive script.

deki-guest pushed a commit to branch master
in repository diffoscope.

commit d733cb47903c54a3c4a699131e1b7bde3e492bbf
Author: Satyam Zode <satyamzode at gmail.com>
Date:   Sun Jun 19 15:09:46 2016 +0530

    Add argument completion feature to diffoscope
    
    This should enable argument completion for diffoscope.
    
    For argument completion `python-argcomplete` module needs to be installed.
    
    If `python-argcomplete` module is not installed and tab is pressed then
    diffoscope returns with status 1.
    
    RangeCompleter class is used as completer for different options having
    integer arguments.
    
    Add documentation for argcomplete in README.rst.
    
    Closes: #826711
---
 README.rst             |  4 ++++
 bin/diffoscope         |  1 +
 diffoscope/__main__.py | 32 +++++++++++++++++++++++++++-----
 3 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/README.rst b/README.rst
index 5288e52..0503c2c 100644
--- a/README.rst
+++ b/README.rst
@@ -59,6 +59,10 @@ Optionally, the following modules will enhance it:
   ``python-magic``. It is built from `file
   <http://www.darwinsys.com/file/>`_.
   Available on Debian and Fedora as ``python3-magic``.
+* ``argcomplete`` is used for argument completion.
+  Available on Debian as ``python3-argcomplete``.
+  Available on Fedora as ``python-argcomplete``.
+  Available on `PyPI <https://pypi.python.org/pypi/argcomplete/>`
 
 The various comparators rely on external commands being available. To
 get a list of them, please run::
diff --git a/bin/diffoscope b/bin/diffoscope
index 2393807..2422b70 100755
--- a/bin/diffoscope
+++ b/bin/diffoscope
@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
+# PYTHON_ARGCOMPLETE_OK
 # -*- coding: utf-8 -*-
 #
 # diffoscope: in-depth comparison of files, archives, and directories
diff --git a/diffoscope/__main__.py b/diffoscope/__main__.py
index ac7913c..d1aa0a5 100644
--- a/diffoscope/__main__.py
+++ b/diffoscope/__main__.py
@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
+# PYTHON_ARGCOMPLETE_OK
 # -*- coding: utf-8 -*-
 #
 # diffoscope: in-depth comparison of files, archives, and directories
@@ -30,6 +31,10 @@ try:
     import tlsh
 except ImportError:
     tlsh = None
+try:
+    import argcomplete
+except ImportError:
+    argcomplete = None
 from diffoscope import logger, VERSION, set_locale, clean_all_temp_files
 import diffoscope.comparators
 from diffoscope.config import Config
@@ -60,25 +65,30 @@ def create_parser():
                         dest='max_report_size', type=int,
                         help='maximum bytes written in report (default: %d)' %
                         Config.general.max_report_size,
-                        default=Config.general.max_report_size)
+                        default=Config.general.max_report_size).completer=RangeCompleter(0,
+                        Config.general.max_report_size, 200000)
     parser.add_argument('--separate-file-diff-size', metavar='BYTES',
                         dest='separate_file_diff_size', type=int,
                         help='diff size to load diff on demand, with --html-dir (default: %d)' %
                         Config.general.separate_file_diff_size,
-                        default=Config.general.separate_file_diff_size)
+                        default=Config.general.separate_file_diff_size).completer=RangeCompleter(0,
+                        Config.general.separate_file_diff_size, 20000)
     parser.add_argument('--max-diff-block-lines', dest='max_diff_block_lines', type=int,
                         help='maximum number of lines per diff block (default: %d)' %
                         Config.general.max_diff_block_lines,
-                        default=Config.general.max_diff_block_lines)
+                        default=Config.general.max_diff_block_lines).completer=RangeCompleter(0,
+                        Config.general.max_diff_block_lines, 5)
     parser.add_argument('--max-diff-input-lines', dest='max_diff_input_lines', type=int,
                         help='maximum number of lines fed to diff (default: %d)' %
                         Config.general.max_diff_input_lines,
-                        default=Config.general.max_diff_input_lines)
+                        default=Config.general.max_diff_input_lines).completer=RangeCompleter(0,
+                        Config.general.max_diff_input_lines, 5000)
     parser.add_argument('--fuzzy-threshold', dest='fuzzy_threshold', type=int,
                         help='threshold for fuzzy-matching '
                              '(0 to disable, %d is default, 400 is high fuzziness)' %
                              (Config.general.fuzzy_threshold),
-                        default=Config.general.fuzzy_threshold)
+                        default=Config.general.fuzzy_threshold).completer=RangeCompleter(0,
+                        400, 20)
     parser.add_argument('--new-file', dest='new_file', action='store_true',
                         help='treat absent files as empty')
     parser.add_argument('--css', metavar='url', dest='css_url',
@@ -89,6 +99,12 @@ def create_parser():
     parser.add_argument('file2', help='second file to compare')
     if not tlsh:
         parser.epilog = 'File renaming detection based on fuzzy-matching is currently disabled. It can be enabled by installing the “tlsh” module available at https://github.com/trendmicro/tlsh'
+    if argcomplete:
+        argcomplete.autocomplete(parser)
+    elif '_ARGCOMPLETE' in os.environ:
+        logger.error('Argument completion requested but the "argcomplete" module is not installed. It can be obtained at https://pypi.python.org/pypi/argcomplete')
+        sys.exit(1)
+
     return parser
 
 
@@ -105,6 +121,12 @@ def make_printer(path):
     if path != '-':
         output.close()
 
+class RangeCompleter(object):
+    def __init__(self, start, end, step):
+        self.choices = range(start, end + 1, step)
+
+    def __call__(self, prefix, **kwargs):
+        return (str(i) for i in self.choices if str(i).startswith(prefix))
 
 class ListToolsAction(argparse.Action):
     def __call__(self, parser, namespace, os_override, option_string=None):

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list