Skip to content

Commit

Permalink
Clean-up argparse usage
Browse files Browse the repository at this point in the history
We frequently pass action="store" (the default), set dest equal to the
default (computed from the option name), and set default equal to the
default. All of these are unnecessary.
  • Loading branch information
gsnedders committed Dec 19, 2024
1 parent b69a9f4 commit 9ff7e54
Show file tree
Hide file tree
Showing 21 changed files with 185 additions and 224 deletions.
5 changes: 3 additions & 2 deletions docs/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ def unlink_source_dirs(created):
def get_parser():
p = argparse.ArgumentParser()
p.add_argument("--type", default="html", help="Output type (default: html)")
p.add_argument("--docker", action="store_true", help="Run inside the docs docker image")
p.add_argument("--serve", default=None, nargs="?", const=8000,
p.add_argument("--docker", action="store_true",
help="Run inside the docs docker image")
p.add_argument("--serve", nargs="?", const=8000,
type=int, help="Run a server on the specified port (default: 8000)")
return p

Expand Down
12 changes: 8 additions & 4 deletions tools/ci/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,14 @@ def get_jobs(paths, **kwargs):

def create_parser():
parser = argparse.ArgumentParser()
parser.add_argument("revish", default=None, help="Commits to consider. Defaults to the commits on the current branch", nargs="?")
parser.add_argument("--all", help="List all jobs unconditionally.", action="store_true")
parser.add_argument("--includes", default=None, help="Jobs to check for. Return code is 0 if all jobs are found, otherwise 1", nargs="*")
parser.add_argument("--json", action="store_true", help="Output jobs as JSON, instead of one per line")
parser.add_argument("revish", nargs="?",
help="Commits to consider. Defaults to the commits on the current branch")
parser.add_argument("--all", action="store_true",
help="List all jobs unconditionally.")
parser.add_argument("--includes", nargs="*",
help="Jobs to check for. Return code is 0 if all jobs are found, otherwise 1")
parser.add_argument("--json", action="store_true",
help="Output jobs as JSON, instead of one per line")
return parser


Expand Down
2 changes: 0 additions & 2 deletions tools/ci/run_tc.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def get_parser():
p = argparse.ArgumentParser()
p.add_argument("--oom-killer",
action="store_true",
default=False,
help="Run userspace OOM killer")
p.add_argument("--hosts",
dest="hosts_file",
Expand All @@ -91,7 +90,6 @@ def get_parser():
default=[],
help="Browsers that will be used in the job")
p.add_argument("--channel",
default=None,
choices=["experimental", "canary", "dev", "nightly", "beta", "stable"],
help="Chrome browser channel")
p.add_argument("--xvfb",
Expand Down
15 changes: 6 additions & 9 deletions tools/ci/taskcluster-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def get_browser_args(product, channel, artifact_path):

def find_wptreport(args):
parser = argparse.ArgumentParser()
parser.add_argument('--log-wptreport', action='store')
parser.add_argument('--log-wptreport')
return parser.parse_known_args(args)[0].log_wptreport


def find_wptscreenshot(args):
parser = argparse.ArgumentParser()
parser.add_argument('--log-wptscreenshot', action='store')
parser.add_argument('--log-wptscreenshot')
return parser.parse_known_args(args)[0].log_wptscreenshot


Expand Down Expand Up @@ -111,17 +111,14 @@ def main(product, channel, commit_range, artifact_path, wpt_args):

if __name__ == "__main__":
parser = argparse.ArgumentParser(description=main.__doc__)
parser.add_argument("--commit-range", action="store",
parser.add_argument("--commit-range",
help="""Git commit range. If specified, this will be
supplied to the `wpt tests-affected` command to
determine the list of test to execute""")
parser.add_argument("--artifact-path", action="store",
default="/home/test/artifacts/",
parser.add_argument("--artifact-path", default="/home/test/artifacts/",
help="Path to store output files")
parser.add_argument("product", action="store",
help="Browser to run tests in")
parser.add_argument("channel", action="store",
help="Channel of the browser")
parser.add_argument("product", help="Browser to run tests in")
parser.add_argument("channel", help="Channel of the browser")
parser.add_argument("wpt_args", nargs="*",
help="Arguments to forward to `wpt run` command")
main(**vars(parser.parse_args())) # type: ignore
11 changes: 5 additions & 6 deletions tools/ci/tc/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@

def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument("--ref", action="store", default="master",
parser.add_argument("--ref", default="master",
help="Branch (in the GitHub repository) or commit to fetch logs for")
parser.add_argument("--artifact-name", action="store", default="wpt_report.json.gz",
parser.add_argument("--artifact-name", default="wpt_report.json.gz",
help="Log type to fetch")
parser.add_argument("--repo-name", action="store", default="web-platform-tests/wpt",
parser.add_argument("--repo-name", default="web-platform-tests/wpt",
help="GitHub repo name in the format owner/repo. "
"This must be the repo from which the Taskcluster run was scheduled "
"(for PRs this is the repo into which the PR would merge)")
parser.add_argument("--token-file", action="store",
help="File containing GitHub token")
parser.add_argument("--out-dir", action="store", default=".",
parser.add_argument("--token-file", help="File containing GitHub token")
parser.add_argument("--out-dir", default=".",
help="Path to save the logfiles")
return parser

Expand Down
2 changes: 1 addition & 1 deletion tools/ci/update_built.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_parser():
parser = ArgumentParser()
parser.add_argument("--list", action="store_true",
help="List suites that can be updated and the related script files")
parser.add_argument("--include", nargs="*", choices=scripts.keys(), default=None,
parser.add_argument("--include", nargs="*", choices=scripts.keys(),
help="Suites to update (default is to update everything)")
return parser

Expand Down
6 changes: 3 additions & 3 deletions tools/docker/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def build(tag="wpt:local", *args, **kwargs):

def parser_push():
parser = argparse.ArgumentParser()
parser.add_argument("--tag", action="store",
parser.add_argument("--tag",
help="Tag to use (default is taken from .taskcluster.yml)")
parser.add_argument("--force", action="store_true",
help="Ignore warnings and push anyway")
Expand Down Expand Up @@ -102,13 +102,13 @@ def push(venv, tag=None, force=False, *args, **kwargs):
def parser_run():
parser = argparse.ArgumentParser()
parser.add_argument("--rebuild", action="store_true", help="Force rebuild of image")
parser.add_argument("--checkout", action="store",
parser.add_argument("--checkout",
help="Revision to checkout in the image. "
"If this is not supplied we mount the wpt checkout on the host as "
"/home/test/web-platform-tests/")
parser.add_argument("--privileged", action="store_true",
help="Run the image in priviledged mode (required for emulators)")
parser.add_argument("--tag", action="store", default="wpt:local",
parser.add_argument("--tag", default="wpt:local",
help="Docker image tag to use (default wpt:local)")
return parser

Expand Down
8 changes: 5 additions & 3 deletions tools/docker/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

def get_args() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument("--delay", action="store", type=float, default=3, help="Initial delay before retry, in seconds")
parser.add_argument("--count", action="store", type=int, default=5, help="Total number of tries")
parser.add_argument("--factor", action="store", type=float, default=2, help="Exponential backoff factor")
parser.add_argument("--delay", type=float, default=3,
help="Initial delay before retry, in seconds")
parser.add_argument("--count", type=int, default=5, help="Total number of tries")
parser.add_argument("--factor", type=float, default=2,
help="Exponential backoff factor")
parser.add_argument("cmd", nargs=argparse.REMAINDER)
return parser

Expand Down
8 changes: 4 additions & 4 deletions tools/manifest/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ def abs_path(path: str) -> str:
def create_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument(
"-v", "--verbose", dest="verbose", action="store_true", default=False,
"-v", "--verbose", action="store_true",
help="Turn on verbose logging")
parser.add_argument(
"-p", "--path", type=abs_path, help="Path to manifest file.")
parser.add_argument(
"--tests-root", type=abs_path, default=wpt_root, help="Path to root of tests.")
parser.add_argument(
"--url-base", action="store", default="/",
"--url-base", default="/",
help="Base url to use as the mount point for tests in this manifest.")
parser.add_argument(
"--cache-root", action="store", default=os.path.join(wpt_root, ".wptcache"),
"--cache-root", default=os.path.join(wpt_root, ".wptcache"),
help="Path in which to store any caches (default <tests_root>/.wptcache/)")
parser.add_argument(
"--no-parallel", dest="parallel", action="store_false", default=True,
"--no-parallel", dest="parallel", action="store_false",
help="Do not parallelize building the manifest")
return parser

Expand Down
14 changes: 7 additions & 7 deletions tools/manifest/testpaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ def create_parser() -> argparse.ArgumentParser:
parser.add_argument(
"-p", "--path", type=abs_path, help="Path to manifest file.")
parser.add_argument(
"--src-root", type=abs_path, default=None, help="Path to root of sourcetree.")
"--src-root", type=abs_path, help="Path to root of sourcetree.")
parser.add_argument(
"--tests-root", type=abs_path, default=wpt_root, help="Path to root of tests.")
parser.add_argument(
"--no-update", dest="update", action="store_false", default=True,
"--no-update", dest="update", action="store_false",
help="Don't update manifest before continuing")
parser.add_argument(
"-r", "--rebuild", action="store_true", default=False,
"-r", "--rebuild", action="store_true",
help="Force a full rebuild of the manifest.")
parser.add_argument(
"--url-base", action="store", default="/",
"--url-base", default="/",
help="Base url to use as the mount point for tests in this manifest.")
parser.add_argument(
"--cache-root", action="store", default=os.path.join(wpt_root, ".wptcache"),
"--cache-root", default=os.path.join(wpt_root, ".wptcache"),
help="Path in which to store any caches (default <tests_root>/.wptcache/)")
parser.add_argument(
"--json", action="store_true", default=False,
"--json", action="store_true",
help="Output as JSON")
parser.add_argument(
"test_ids", action="store", nargs="+",
"test_ids", nargs="+",
help="Test ids for which to get paths")
return parser

Expand Down
12 changes: 6 additions & 6 deletions tools/manifest/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,26 @@ def abs_path(path: str) -> str:
def create_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument(
"-v", "--verbose", dest="verbose", action="store_true", default=False,
"-v", "--verbose", action="store_true",
help="Turn on verbose logging")
parser.add_argument(
"-p", "--path", type=abs_path, help="Path to manifest file.")
parser.add_argument(
"--tests-root", type=abs_path, default=wpt_root, help="Path to root of tests.")
parser.add_argument(
"-r", "--rebuild", action="store_true", default=False,
"-r", "--rebuild", action="store_true",
help="Force a full rebuild of the manifest.")
parser.add_argument(
"--url-base", action="store", default="/",
"--url-base", default="/",
help="Base url to use as the mount point for tests in this manifest.")
parser.add_argument(
"--no-download", dest="download", action="store_false", default=True,
"--no-download", dest="download", action="store_false",
help="Never attempt to download the manifest.")
parser.add_argument(
"--cache-root", action="store", default=os.path.join(wpt_root, ".wptcache"),
"--cache-root", default=os.path.join(wpt_root, ".wptcache"),
help="Path in which to store any caches (default <tests_root>/.wptcache/)")
parser.add_argument(
"--no-parallel", dest="parallel", action="store_false", default=True,
"--no-parallel", dest="parallel", action="store_false",
help="Do not parallelize building the manifest")
return parser

Expand Down
18 changes: 9 additions & 9 deletions tools/serve/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,25 +1356,25 @@ def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument("--latency", type=int,
help="Artificial latency to add before sending http responses, in ms")
parser.add_argument("--config", action="store", dest="config_path",
parser.add_argument("--config", dest="config_path",
help="Path to external config file")
parser.add_argument("--doc_root", action="store", dest="doc_root",
help="Path to document root. Overrides config.")
parser.add_argument("--ws_doc_root", action="store", dest="ws_doc_root",
parser.add_argument("--doc_root", help="Path to document root. Overrides config.")
parser.add_argument("--ws_doc_root",
help="Path to WebSockets document root. Overrides config.")
parser.add_argument("--ws_extra", action="append", dest="ws_extra", default=[],
parser.add_argument("--ws_extra", action="append", default=[],
help="Path to extra directory containing ws handlers. Overrides config.")
parser.add_argument("--inject-script", default=None,
parser.add_argument("--inject-script",
help="Path to script file to inject, useful for testing polyfills.")
parser.add_argument("--alias_file", action="store", dest="alias_file",
parser.add_argument("--alias_file",
help="File with entries for aliases/multiple doc roots. In form of `/ALIAS_NAME/, DOC_ROOT\\n`")
parser.add_argument("--h2", action="store_true", dest="h2", default=None,
parser.add_argument("--h2", action="store_true", default=None,
help=argparse.SUPPRESS)
parser.add_argument("--no-h2", action="store_false", dest="h2", default=None,
help="Disable the HTTP/2.0 server")
parser.add_argument("--webtransport-h3", action="store_true",
help="Enable WebTransport over HTTP/3 server")
parser.add_argument("--exit-after-start", action="store_true", help="Exit after starting servers")
parser.add_argument("--exit-after-start", action="store_true",
help="Exit after starting servers")
parser.add_argument("--verbose", action="store_true", help="Enable verbose logging")
parser.set_defaults(report=False)
parser.set_defaults(is_wave=False)
Expand Down
2 changes: 1 addition & 1 deletion tools/serve/wave.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ConfigBuilder(serve.ConfigBuilder):
def get_parser():
parser = serve.get_parser()
# Added wave specific arguments
parser.add_argument("--report", action="store_true", dest="report",
parser.add_argument("--report", action="store_true",
help="Flag for enabling the WPTReporting server")
return parser

Expand Down
6 changes: 3 additions & 3 deletions tools/wpt/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def do_delayed_imports(paths):

def get_parser_install():
parser = argparse.ArgumentParser()
parser.add_argument("--path", dest="dest", action="store", default=None,
parser.add_argument("--path", dest="dest",
help="Root path to use for emulator tooling")
parser.add_argument("--reinstall", action="store_true", default=False,
parser.add_argument("--reinstall", action="store_true",
help="Force reinstall even if the emulator already exists")
parser.add_argument("--prompt", action="store_true",
help="Enable confirmation prompts")
Expand All @@ -74,7 +74,7 @@ def get_parser_install():

def get_parser_start():
parser = get_parser_install()
parser.add_argument("--device-serial", action="store", default=None,
parser.add_argument("--device-serial",
help="Device serial number for Android emulator, if not emulator-5554")
return parser

Expand Down
6 changes: 3 additions & 3 deletions tools/wpt/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_parser():
p = argparse.ArgumentParser()
p.add_argument("--no-editor", action="store_true",
help="Don't try to open the test in an editor")
p.add_argument("-e", "--editor", action="store", help="Editor to use")
p.add_argument("-e", "--editor", help="Editor to use")
p.add_argument("--long-timeout", action="store_true",
help="Test should be given a long timeout (typically 60s rather than 10s, but varies depending on environment)")
p.add_argument("--overwrite", action="store_true",
Expand All @@ -42,9 +42,9 @@ def get_parser():
help="Create a mismatch reftest")
p.add_argument("--wait", action="store_true",
help="Create a reftest that waits until takeScreenshot() is called")
p.add_argument("--tests-root", action="store", default=os.path.join(here, "..", ".."),
p.add_argument("--tests-root", default=os.path.join(here, "..", ".."),
help="Path to the root of the wpt directory")
p.add_argument("path", action="store", help="Path to the test file")
p.add_argument("path", help="Path to the test file")
return p


Expand Down
6 changes: 3 additions & 3 deletions tools/wpt/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

channel_args = argparse.ArgumentParser(add_help=False)
channel_args.add_argument('--channel', choices=channel_by_name.keys(),
default='nightly', action='store',
default='nightly',
help='''
Name of browser release channel (default: nightly). "stable" and "release" are
synonyms for the latest browser stable release; "beta" is the beta release;
Expand All @@ -52,12 +52,12 @@ def get_parser():
help='name of component')
parser.add_argument('--download-only', action="store_true",
help="Download the selected component but don't install it")
parser.add_argument('--rename', action="store", default=None,
parser.add_argument('--rename',
help="Filename, excluding extension for downloaded archive "
"(only with --download-only)")
parser.add_argument('-d', '--destination',
help='filesystem directory to place the component')
parser.add_argument('--revision', default=None,
parser.add_argument('--revision',
help='Chromium revision to install from snapshots')
return parser

Expand Down
Loading

0 comments on commit 9ff7e54

Please sign in to comment.