Skip to content

Commit

Permalink
Print full help message for incorrect parameters
Browse files Browse the repository at this point in the history
The usage message printed automatically by argparse is terse and
confusing. We also already print the full help message in other cases.

Fixes #56
  • Loading branch information
Matoking committed May 9, 2020
1 parent 9809712 commit 5a1500f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added
- Print a warning if both `steamapps` and `SteamApps` directories are found inside the same library directory

### Changed
- Print full help message when incorrect parameters are provided.

## [1.4.1] - 2020-02-17
### Fixed
- Fixed crash caused by Steam library paths containing special characters
Expand Down
18 changes: 15 additions & 3 deletions src/protontricks/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,36 @@ def enable_logging(info=False):
format="%(name)s (%(levelname)s): %(message)s")


class CustomArgumentParser(argparse.ArgumentParser):
"""
Custom argument parser that prints the full help message
when incorrect parameters are provided
"""
def error(self, message):
self.print_help(sys.stderr)
args = {'prog': self.prog, 'message': message}
self.exit(2, '%(prog)s: error: %(message)s\n' % args)


def main(args=None):
"""
'protontricks' script entrypoint
"""
parser = argparse.ArgumentParser(
parser = CustomArgumentParser(
description=(
"Wrapper for running Winetricks commands for "
"Steam Play/Proton games.\n"
"\n"
"Usage:\n"
"\n"
"Run winetricks for game with APPID\n"
"Run winetricks for game with APPID. "
"COMMAND is passed directly to winetricks as-is.\n"
"$ protontricks APPID COMMAND\n"
"\n"
"Search installed games to find the APPID\n"
"$ protontricks -s GAME_NAME\n"
"\n"
"Launch the Protontricks GUI\n"
"Use Protontricks GUI to select the game\n"
"$ protontricks --gui\n"
"\n"
"Environment variables:\n"
Expand Down
13 changes: 13 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,16 @@ def test_search_shortcut(

assert "Non-Steam shortcut: fakegame.exe (4149337689)" in result
assert "Non-Steam shortcut: fakegame.exe (4136117770)" in result


def test_cli_error_help(cli):
"""
Ensure that the full help message is printed when an incorrect argument
is provided
"""
_, stderr = cli(["--nothing"], expect_exit=True, include_stderr=True)

# Usage message
assert "[-h] [--verbose]" in stderr
# Help message
assert "positional arguments:" in stderr

0 comments on commit 5a1500f

Please sign in to comment.