33"""
44
55import argparse
6+ import logging
67import sys
78
9+ from progressive_automations_python import __version__
10+
11+ __author__ = "Sterling G. Baird"
12+ __copyright__ = "Sterling G. Baird"
13+ __license__ = "MIT"
14+
15+ _logger = logging .getLogger (__name__ )
16+
17+
18+ # ---- Python API ----
19+ # The functions defined in this section can be imported by users in their
20+ # Python scripts/interactive interpreter, e.g. via
21+ # `from progressive_automations_python.skeleton import fib`,
22+ # when using this Python module as a library.
23+
824
925def test_movement (direction : str ):
1026 """Test UP or DOWN movement for 2 seconds"""
@@ -55,17 +71,22 @@ def show_status():
5571 return 1
5672
5773
74+ # ---- CLI ----
75+ # The functions defined in this section are wrappers around the main Python
76+ # API allowing them to be called directly from the terminal as a CLI
77+ # executable/script.
5878
5979
80+ def parse_args (args ):
81+ """Parse command line parameters
6082
83+ Args:
84+ args (List[str]): command line parameters as list of strings
85+ (for example ``["--help"]``).
6186
62-
63-
64-
65-
66-
67- def main ():
68- """Main CLI entry point"""
87+ Returns:
88+ :obj:`argparse.Namespace`: command line parameters namespace
89+ """
6990 parser = argparse .ArgumentParser (
7091 description = "Progressive Automations Desk Lifter Control" ,
7192 formatter_class = argparse .RawDescriptionHelpFormatter ,
@@ -76,7 +97,11 @@ def main():
7697 progressive_automations_python --status
7798 """
7899 )
79-
100+ parser .add_argument (
101+ "--version" ,
102+ action = "version" ,
103+ version = f"progressive-automations-python { __version__ } " ,
104+ )
80105 parser .add_argument (
81106 "--test" ,
82107 type = str ,
@@ -89,18 +114,78 @@ def main():
89114 action = "store_true" ,
90115 help = "Show current duty cycle status"
91116 )
92-
93- args = parser .parse_args ()
117+ parser .add_argument (
118+ "-v" ,
119+ "--verbose" ,
120+ dest = "loglevel" ,
121+ help = "set loglevel to INFO" ,
122+ action = "store_const" ,
123+ const = logging .INFO ,
124+ )
125+ parser .add_argument (
126+ "-vv" ,
127+ "--very-verbose" ,
128+ dest = "loglevel" ,
129+ help = "set loglevel to DEBUG" ,
130+ action = "store_const" ,
131+ const = logging .DEBUG ,
132+ )
133+ return parser .parse_args (args )
134+
135+
136+ def setup_logging (loglevel ):
137+ """Setup basic logging
138+
139+ Args:
140+ loglevel (int): minimum loglevel for emitting messages
141+ """
142+ logformat = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s"
143+ logging .basicConfig (
144+ level = loglevel , stream = sys .stdout , format = logformat , datefmt = "%Y-%m-%d %H:%M:%S"
145+ )
146+
147+
148+ def main (args ):
149+ """Wrapper allowing CLI functions to be called with string arguments in a CLI fashion
150+
151+ Args:
152+ args (List[str]): command line parameters as list of strings
153+ (for example ``["--verbose", "--test", "UP"]``).
154+ """
155+ args = parse_args (args )
156+ setup_logging (args .loglevel )
157+ _logger .debug ("Starting desk lifter control..." )
94158
95159 # Handle commands
96160 if args .test :
97- return test_movement (args .test )
161+ result = test_movement (args .test )
162+ sys .exit (result )
98163 elif args .status :
99- return show_status ()
164+ result = show_status ()
165+ sys .exit (result )
100166 else :
101- parser .print_help ()
102- return 0
167+ # No command specified, print help
168+ print ("No command specified. Use --help for usage information." )
169+ sys .exit (0 )
170+
171+
172+ def run ():
173+ """Calls :func:`main` passing the CLI arguments extracted from :obj:`sys.argv`
174+
175+ This function can be used as entry point to create console scripts with setuptools.
176+ """
177+ main (sys .argv [1 :])
103178
104179
105180if __name__ == "__main__" :
106- sys .exit (main ())
181+ # ^ This is a guard statement that will prevent the following code from
182+ # being executed in the case someone imports this file instead of
183+ # executing it as a script.
184+ # https://docs.python.org/3/library/__main__.html
185+
186+ # After installing your project with pip, users can also run your Python
187+ # modules as scripts via the ``-m`` flag, as defined in PEP 338::
188+ #
189+ # python -m progressive_automations_python.skeleton 42
190+ #
191+ run ()
0 commit comments