comparison launch @ 37:ce67eac10fc7 draft header-includes

Allow global character encoding specification.
author David Barts <n5jrn@me.com>
date Tue, 28 May 2019 17:08:36 -0700
parents e93e5e746cc5
children df27cf08c093
comparison
equal deleted inserted replaced
36:4ed261056057 37:ce67eac10fc7
5 5
6 # I m p o r t s 6 # I m p o r t s
7 7
8 import os, sys 8 import os, sys
9 from argparse import ArgumentParser 9 from argparse import ArgumentParser
10 from tincan import launch 10 from tincan import launch, ENCODING
11 11
12 # V a r i a b l e s 12 # V a r i a b l e s
13 13
14 MYNAME = os.path.basename(sys.argv[0]) 14 MYNAME = os.path.basename(sys.argv[0])
15 15
16 # M a i n P r o g r a m 16 # M a i n P r o g r a m
17 17
18 parser = ArgumentParser(prog=sys.argv[0], usage="%(prog)s [options] [directory [path]]") 18 parser = ArgumentParser(prog=sys.argv[0], usage="%(prog)s [options] [directory [path]]")
19 opt = parser.add_argument 19 opt = parser.add_argument
20 opt("-b", "--bind", default="localhost", help="address to bind to") 20 opt("-b", "--bind", default="localhost", help="address to bind to (default: localhost)")
21 opt("-d", "--debug", action="store_true", help="enable debug mode") 21 opt("-d", "--debug", action="store_true", help="enable debug mode")
22 opt("-e", "--encoding", default=ENCODING, help="encoding to use (default {0})".format(ENCODING))
22 opt("-f", "--force", action="store_true", help="do not abort on errors") 23 opt("-f", "--force", action="store_true", help="do not abort on errors")
23 opt("-p", "--port", default=8080, help="port to listen on") 24 opt("-p", "--port", default=8080, help="port to listen on (default: 8080)")
24 opt("directory", default=".", help="directory to serve", nargs='?') 25 opt("directory", default=".", help="directory to serve", nargs='?')
25 opt("path", default="/", help="URL path to serve", nargs='?') 26 opt("path", default="/", help="URL path to serve", nargs='?')
26 args = parser.parse_args(sys.argv[1:]) 27 args = parser.parse_args(sys.argv[1:])
27 app, errors = launch(fsroot=args.directory, urlroot=args.path, debug=args.debug) 28 app, errors = launch(fsroot=args.directory, urlroot=args.path, debug=args.debug,
29 encoding=args.encoding)
28 if errors: 30 if errors:
29 action = "continuing" if args.force else "aborting" 31 action = "continuing" if args.force else "aborting"
30 sys.stderr.write("{0}: {1} error{2} detected, {3}\n".format( 32 sys.stderr.write("{0}: {1} error{2} detected, {3}\n".format(
31 MYNAME, errors, "" if errors == 1 else "s", action)) 33 MYNAME, errors, "" if errors == 1 else "s", action))
32 if not args.force: sys.exit(1) 34 if not args.force: sys.exit(1)