Skip to content

Commit

Permalink
Potential fix for issue #4
Browse files Browse the repository at this point in the history
 * workaround pallets/click#365
   by suclassing click.Command.main() to force the prog_name
   to self.name.
 * also refactored the usage display for
   pallets/click#393
  • Loading branch information
pombredanne committed Jul 25, 2015
1 parent 3aa5a30 commit 586a7c7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/scancode/cli.py
Expand Up @@ -205,19 +205,29 @@ def print_version(ctx, param, value):
'''


short_help = '''Usage: scancode [OPTIONS] <input> <output_file>
short_help = '''
Try 'scancode --help' for help on options and arguments.'''


formats = ['json', 'html', 'html-app']


class ScanCommand(click.Command):
"""
Workaround click 4.0 bug https://github.com/mitsuhiko/click/issues/365
"""
def get_usage(self, ctx):
return short_help
"""
Ensure that usage points to the --help option explicitly.
Workaround click issue https://github.com/mitsuhiko/click/issues/393
"""
return click.Command.get_usage(self, ctx) + short_help

def main(self, args=None, prog_name=None, complete_var=None,
standalone_mode=True, **extra):
"""
Workaround click 4.0 bug https://github.com/mitsuhiko/click/issues/365
"""
return click.Command.main(self, args=args, prog_name=self.name,
complete_var=complete_var,
standalone_mode=standalone_mode, **extra)


@click.command(name='scancode', epilog=epilog_text, cls=ScanCommand)
Expand All @@ -240,7 +250,7 @@ def get_usage(self, ctx):
help=('Show information about ScanCode and licensing and exit.'))
@click.option('--version', is_flag=True, is_eager=True, callback=print_version,
help=('Show the version and exit.'))
def scancode(ctx, input, output_file, extract, copyright, license, format, verbose, *args, **kwargs):
def scancode(ctx, input, output_file, extract, copyright, license, format, verbose, *args, **kwargs): # @ReservedAssignment
"""scan the <input> file or directory for origin and license and save results to the <output_file>.
The scan results are printed on terminal if <output_file> is not provided.
Expand All @@ -263,8 +273,8 @@ def scancode(ctx, input, output_file, extract, copyright, license, format, verbo

# Default scan when no options is provided
if not any(scans):
copyright = True
license = True
copyright = True # @ReservedAssignment
license = True # @ReservedAssignment

if copyright or license:
click.secho('Scanning files...', fg='green')
Expand Down Expand Up @@ -309,7 +319,7 @@ def scancode(ctx, input, output_file, extract, copyright, license, format, verbo
click.secho('Scanning done.', fg='green')


def scan_one(input_file, copyright, license, verbose=False):
def scan_one(input_file, copyright, license, verbose=False): # @ReservedAssignment
"""
Scan one file and return scanned data.
"""
Expand All @@ -329,7 +339,7 @@ def scan_one(input_file, copyright, license, verbose=False):
return data


def extract_with_progress(input, verbose=False):
def extract_with_progress(input, verbose=False): # @ReservedAssignment
"""
Extract archives and display progress.
"""
Expand Down
16 changes: 16 additions & 0 deletions tests/scancode/test_cli.py
Expand Up @@ -135,3 +135,19 @@ def test_scancode_skip_vcs_files_and_dirs_by_default(self):
assert 1 == scan_result['count']
scan_loc = as_posixpath(scan_result['results'][0]['location'])
assert scan_loc.endswith('vcs.tgz/vcs/test.txt')

def test_usage_and_help_return_a_correct_script_name_on_all_platforms(self):
runner = CliRunner()
result = runner.invoke(cli.scancode, ['--help'])
assert 'Usage: scancode [OPTIONS]' in result.output
# this was showing up on Windows
assert 'scancode-script.py' not in result.output

result = runner.invoke(cli.scancode, [])
assert 'Usage: scancode [OPTIONS]' in result.output
# this was showing up on Windows
assert 'scancode-script.py' not in result.output

result = runner.invoke(cli.scancode, ['-xyz'])
# this was showing up on Windows
assert 'scancode-script.py' not in result.output

0 comments on commit 586a7c7

Please sign in to comment.