always parse args from sys.argv

This commit is contained in:
Alan Hamlett 2015-08-12 10:28:01 -07:00
parent 271e457654
commit f818a64d3d
5 changed files with 26 additions and 32 deletions

View file

@ -1,10 +1,10 @@
usage: nosetests [-h] --file file [--key KEY] [--write] [--plugin PLUGIN]
[--time time] [--lineno LINENO] [--cursorpos CURSORPOS]
[--notfile] [--proxy PROXY] [--project PROJECT]
[--alternate-project ALTERNATE_PROJECT] [--hostname HOSTNAME]
[--disableoffline] [--hidefilenames] [--exclude EXCLUDE]
[--include INCLUDE] [--logfile LOGFILE] [--apiurl API_URL]
[--config CONFIG] [--verbose] [--version]
usage: wakatime [-h] --file file [--key KEY] [--write] [--plugin PLUGIN]
[--time time] [--lineno LINENO] [--cursorpos CURSORPOS]
[--notfile] [--proxy PROXY] [--project PROJECT]
[--alternate-project ALTERNATE_PROJECT] [--hostname HOSTNAME]
[--disableoffline] [--hidefilenames] [--exclude EXCLUDE]
[--include INCLUDE] [--logfile LOGFILE] [--apiurl API_URL]
[--config CONFIG] [--verbose] [--version]
Common interface for the WakaTime api.

View file

@ -1,8 +1,8 @@
usage: nosetests [-h] --file file [--key KEY] [--write] [--plugin PLUGIN]
[--time time] [--lineno LINENO] [--cursorpos CURSORPOS]
[--notfile] [--proxy PROXY] [--project PROJECT]
[--alternate-project ALTERNATE_PROJECT] [--hostname HOSTNAME]
[--disableoffline] [--hidefilenames] [--exclude EXCLUDE]
[--include INCLUDE] [--logfile LOGFILE] [--apiurl API_URL]
[--config CONFIG] [--verbose] [--version]
nosetests: error: Missing api key
usage: wakatime [-h] --file file [--key KEY] [--write] [--plugin PLUGIN]
[--time time] [--lineno LINENO] [--cursorpos CURSORPOS]
[--notfile] [--proxy PROXY] [--project PROJECT]
[--alternate-project ALTERNATE_PROJECT] [--hostname HOSTNAME]
[--disableoffline] [--hidefilenames] [--exclude EXCLUDE]
[--include INCLUDE] [--logfile LOGFILE] [--apiurl API_URL]
[--config CONFIG] [--verbose] [--version]
wakatime: error: Missing api key

View file

@ -18,7 +18,7 @@ from wakatime.base import main
class BaseTestCase(utils.TestCase):
def test_help_contents(self, mock_requests):
args = ['', '--help']
args = ['--help']
with self.assertRaises(SystemExit):
main(args)
expected_stdout = open('tests/samples/output/test_help_contents').read()
@ -29,7 +29,7 @@ class BaseTestCase(utils.TestCase):
response = Response()
response.status_code = 201
mock_requests.return_value = response
args = ['', '--file', 'tests/samples/emptyfile.txt', '--key', '123', '--config', 'foo']
args = ['--file', 'tests/samples/emptyfile.txt', '--key', '123', '--config', 'foo']
retval = main(args)
self.assertEquals(retval, 0)
expected_stdout = u("Error: Could not read from config file foo\n")
@ -37,7 +37,7 @@ class BaseTestCase(utils.TestCase):
self.assertEquals(sys.stderr.getvalue(), '')
def test_missing_config_file(self, mock_requests):
args = ['', '--file', 'tests/samples/emptyfile.txt', '--config', 'foo']
args = ['--file', 'tests/samples/emptyfile.txt', '--config', 'foo']
with self.assertRaises(SystemExit):
main(args)
expected_stdout = u("Error: Could not read from config file foo\n")
@ -49,14 +49,14 @@ class BaseTestCase(utils.TestCase):
response = Response()
response.status_code = 201
mock_requests.return_value = response
args = ['', '--file', 'tests/samples/emptyfile.txt', '--config', 'tests/samples/sample.cfg']
args = ['--file', 'tests/samples/emptyfile.txt', '--config', 'tests/samples/sample.cfg']
retval = main(args)
self.assertEquals(retval, 0)
self.assertEquals(sys.stdout.getvalue(), '')
self.assertEquals(sys.stderr.getvalue(), '')
def test_parse_bad_config_file(self, mock_requests):
args = ['', '--file', 'tests/samples/emptyfile.txt', '--config', 'tests/samples/bad_config.cfg']
args = ['--file', 'tests/samples/emptyfile.txt', '--config', 'tests/samples/bad_config.cfg']
retval = main(args)
self.assertEquals(retval, 103)
self.assertIn('ParsingError', sys.stdout.getvalue())

View file

@ -79,17 +79,12 @@ def parseConfigFile(configFile=None):
return configs
def parseArguments(argv):
def parseArguments():
"""Parse command line arguments and configs from ~/.wakatime.cfg.
Command line arguments take precedence over config file settings.
Returns instances of ArgumentParser and SafeConfigParser.
"""
try:
sys.argv
except AttributeError:
sys.argv = argv
# define supported command line arguments
parser = argparse.ArgumentParser(
description='Common interface for the WakaTime api.')
@ -151,7 +146,7 @@ def parseArguments(argv):
parser.add_argument('--version', action='version', version=__version__)
# parse command line arguments
args = parser.parse_args(args=argv[1:])
args = parser.parse_args()
# use current unix epoch timestamp by default
if not args.timestamp:
@ -384,11 +379,10 @@ def send_heartbeat(project=None, branch=None, hostname=None, stats={}, key=None,
return False
def main(argv=None):
if not argv:
argv = sys.argv
def main(argv):
sys.argv = ['wakatime'] + argv
args, configs = parseArguments(argv)
args, configs = parseArguments()
if configs is None:
return 103 # config file parsing error

View file

@ -32,4 +32,4 @@ except TypeError:
if __name__ == '__main__':
sys.exit(wakatime.main(sys.argv))
sys.exit(wakatime.main(sys.argv[1:]))