Skip to content

Commit

Permalink
A less invasive --app flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kgaughan committed Nov 24, 2024
1 parent 2a1524a commit c970e55
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/waitress/adjustments.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,12 @@ def parse_args(cls, argv):
else:
long_opts.append(opt + "=")

long_opts.append("app=")

kw = {
"help": False,
"call": False,
"app": None,
}

opts, args = getopt.getopt(argv, "", long_opts)
Expand All @@ -482,6 +485,9 @@ def parse_args(cls, argv):
else:
kw[param] = value

if kw["app"] is None and len(args) > 0:
kw["app"] = args.pop(0)

return kw, args

@classmethod
Expand Down
8 changes: 4 additions & 4 deletions src/waitress/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ def run(argv=sys.argv, _serve=serve):
show_help(sys.stdout, name)
return 0

if len(args) != 1:
show_help(sys.stderr, name, "Specify one application only")
if kw["app"] is None:
show_help(sys.stderr, name, "Specify an application")
return 1

# set a default level for the logger only if it hasn't been set explicitly
Expand All @@ -323,7 +323,7 @@ def run(argv=sys.argv, _serve=serve):

# Get the WSGI function.
try:
app = pkgutil.resolve_name(args[0])
app = pkgutil.resolve_name(kw["app"])
except (ValueError, ImportError, AttributeError) as exc:
show_help(sys.stderr, name, str(exc))
show_exception(sys.stderr)
Expand All @@ -332,7 +332,7 @@ def run(argv=sys.argv, _serve=serve):
app = app()

# These arguments are specific to the runner, not waitress itself.
del kw["call"], kw["help"]
del kw["call"], kw["help"], kw["app"]

_serve(app, **kw)
return 0
8 changes: 4 additions & 4 deletions tests/test_adjustments.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,22 +396,22 @@ def assertDictContainsSubset(self, subset, dictionary):

def test_noargs(self):
opts, args = self.parse([])
self.assertDictEqual(opts, {"call": False, "help": False})
self.assertDictEqual(opts, {"call": False, "help": False, "app": None})
self.assertSequenceEqual(args, [])

def test_help(self):
opts, args = self.parse(["--help"])
self.assertDictEqual(opts, {"call": False, "help": True})
self.assertDictEqual(opts, {"call": False, "help": True, "app": None})
self.assertSequenceEqual(args, [])

def test_call(self):
opts, args = self.parse(["--call"])
self.assertDictEqual(opts, {"call": True, "help": False})
self.assertDictEqual(opts, {"call": True, "help": False, "app": None})
self.assertSequenceEqual(args, [])

def test_both(self):
opts, args = self.parse(["--call", "--help"])
self.assertDictEqual(opts, {"call": True, "help": True})
self.assertDictEqual(opts, {"call": True, "help": True, "app": None})
self.assertSequenceEqual(args, [])

def test_positive_boolean(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def test_help(self):
self.match_output(["--help"], 0, "^Usage:\n\n waitress-serve")

def test_no_app(self):
self.match_output([], 1, "^Error: Specify one application only")
self.match_output([], 1, "^Error: Specify an application")

def test_multiple_apps_app(self):
self.match_output(["a:a", "b:b"], 1, "^Error: Specify one application only")
self.match_output(["a:a", "b:b"], 1, "^Error: No module named 'a'")

def test_bad_apps_app(self):
self.match_output(["a"], 1, "^Error: No module named 'a'")
Expand Down

0 comments on commit c970e55

Please sign in to comment.