mirror of https://github.com/home-assistant/core
Refactor __main__ to use ArgParse
This commit is contained in:
parent
aa80841519
commit
9f24101348
|
@ -5,4 +5,4 @@ VOLUME /config
|
||||||
|
|
||||||
EXPOSE 8123
|
EXPOSE 8123
|
||||||
|
|
||||||
CMD [ "python", "-m", "homeassistant", "--docker" ]
|
CMD [ "python", "-m", "homeassistant", "--config", "/config" ]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import argparse
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from homeassistant import bootstrap
|
from homeassistant import bootstrap
|
||||||
|
@ -16,26 +17,35 @@ except ImportError:
|
||||||
from homeassistant import bootstrap
|
from homeassistant import bootstrap
|
||||||
|
|
||||||
|
|
||||||
ARG_RUN_TESTS = "--run-tests"
|
|
||||||
ARG_DOCKER = '--docker'
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
""" Starts Home Assistant. Will create demo config if no config found. """
|
""" Starts Home Assistant. Will create demo config if no config found. """
|
||||||
|
tasks = ['serve', 'test']
|
||||||
|
|
||||||
# Do we want to run the tests?
|
parser = argparse.ArgumentParser()
|
||||||
if ARG_RUN_TESTS in sys.argv:
|
parser.add_argument(
|
||||||
sys.argv.remove(ARG_RUN_TESTS)
|
'-c', '--config',
|
||||||
|
metavar='path_to_config_dir',
|
||||||
|
default="config",
|
||||||
|
help="Directory that contains the Home Assistant configuration")
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-t', '--task',
|
||||||
|
default=tasks[0],
|
||||||
|
choices=tasks,
|
||||||
|
help="Task to execute. Defaults to serve.")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.task == tasks[1]:
|
||||||
|
# unittest does not like our command line arguments, remove them
|
||||||
|
sys.argv[1:] = []
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
unittest.main(module='homeassistant.test')
|
unittest.main(module='homeassistant.test')
|
||||||
|
|
||||||
# Within Docker we load the config from a different path
|
|
||||||
if ARG_DOCKER in sys.argv:
|
|
||||||
config_path = '/config/home-assistant.conf'
|
|
||||||
else:
|
else:
|
||||||
config_path = 'config/home-assistant.conf'
|
config_path = os.path.join(args.config, 'home-assistant.conf')
|
||||||
|
|
||||||
# Ensure a config file exists to make first time usage easier
|
# Ensure a config file exists to make first time usage easier
|
||||||
if not os.path.isfile(config_path):
|
if not os.path.isfile(config_path):
|
||||||
|
|
Loading…
Reference in New Issue