Skip to content

Commit

Permalink
Upgraded to use argparse instead of getopt #204
Browse files Browse the repository at this point in the history
Allows optional parameters - used for ttk
  • Loading branch information
jarvisteach committed Aug 18, 2017
1 parent ae42985 commit c296d2e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 57 deletions.
80 changes: 30 additions & 50 deletions appJar/appjar.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import datetime # datepicker & image
import logging # python's logger
import inspect # for logging
import argparse # argument parser

import __main__ as theMain
from platform import system as platform
Expand Down Expand Up @@ -436,7 +437,7 @@ def __init__(self, title=None, geom=None, warn=None, debug=None, handleArgs=True

# check any command line arguments
self.language = None
opts = self.__handleArgs() if handleArgs else None
args = self.__handleArgs() if handleArgs else None

# warn if we're in an untested mode
self.__checkMode()
Expand All @@ -450,32 +451,19 @@ def __init__(self, title=None, geom=None, warn=None, debug=None, handleArgs=True
self.warn("Cannot set logging level in __init__. You should use .setLogLevel()")

# process any command line arguments
if opts is not None:
# process these first
if "-V" in opts or "--version" in opts:
print("\nappJar Information\n\t"+
gui.SHOW_VERSION().replace("\n", "\n\t"))
sys.exit()
elif "-h" in opts or "--help" in opts:
self.__showHelp()
sys.exit()

# if we didn't exit, next set the log level
for opt, arg in opts:
if opt == "-c": gui.setLogLevel("CRITICAL")
elif opt == "-e": gui.setLogLevel("ERROR")
elif opt == "-w": gui.setLogLevel("WARNING")
elif opt == "-i": gui.setLogLevel("INFO")
elif opt == "-d": gui.setLogLevel("DEBUG")

# process the rest
for opt, arg in opts:
if opt in ["-l", "--language"]:
self.language = arg
elif opt in ["-t", "--ttk"]:
self.useTtk()
elif opt in ["--theme"]:
self.ttkStyle = arg
if handleArgs:
if args.c: gui.setLogLevel("CRITICAL")
elif args.e: gui.setLogLevel("ERROR")
elif args.w: gui.setLogLevel("WARNING")
elif args.i: gui.setLogLevel("INFO")
elif args.d: gui.setLogLevel("DEBUG")

self.language = args.l
if args.f: gui.setLogFile(args.f)
if args.ttk:
self.useTtk()
if args.ttk is not True:
self.ttkStyle = args.ttk

# a stack to hold containers as being built
# done here, as initArrays is called elsewhere - to reset the gubbins
Expand Down Expand Up @@ -615,30 +603,22 @@ def __init__(self, title=None, geom=None, warn=None, debug=None, handleArgs=True
if self.ttkStyle is not None:
self.setTtkTheme(self.ttkStyle)

# function to check command line parameters
def __showHelp(self):
print("\nUsage:\n " + sys.argv[0] + " [-cewid] [-h --help] [-V --version] [-l --language] <language> [-f --file] <filename> [-t --ttk] [--theme] <theme>")
print("\nOptions:")
print(" -h, --help:\t\t\tShow help.")
print(" -V, --version:\t\tShow version information.")
print(" -l, --language <language>:\tSet the language file to use.")
print(" -t,--ttk:\t\t\tEnable ttk.")
print(" --theme:\t\t\tSet a ttk theme.")
print(" -f,--file <filename>:\t\tSet the log file to use.")
print(" -c:\t\t\t\tOnly log CRITICAL messages.")
print(" -e:\t\t\t\tLog ERROR messages and above.")
print(" -w:\t\t\t\tLog WARNING messages and above.")
print(" -i:\t\t\t\tLog INFO messages and above.")
print(" -d:\t\t\t\tLog DEBUG messages and above.")

def __handleArgs(self):
import getopt
try:
opts, args = getopt.getopt(sys.argv[1:], "cewidVhtl:f:", ["version", "language=", "file=", "help", "ttk", "theme="])
return opts
except getopt.GetoptError:
self.__showHelp()
sys.exit(2)
parser = argparse.ArgumentParser(
description="appJar - the easiest way to create GUIs in python",
epilog="For more information, go to: http://appJar.info"
)
parser.add_argument("-v", "--version", action="version", version=gui.SHOW_VERSION(), help="show version information and exit")
logGroup = parser.add_mutually_exclusive_group()
logGroup.add_argument("-c", action="store_const", const=True, help="only log CRITICAL messages")
logGroup.add_argument("-e", action="store_const", const=True, help="log ERROR messages and above")
logGroup.add_argument("-w", action="store_const", const=True, help="log WARNING messages and above")
logGroup.add_argument("-i", action="store_const", const=True, help="log INFO messages and above")
logGroup.add_argument("-d", action="store_const", const=True, help="log DEBUG messages and above")
parser.add_argument("-l", metavar="LANGUAGE.ini", help="set a language file to use")
parser.add_argument("-f", metavar="file.log", help="set a log file to use")
parser.add_argument("--ttk", metavar="THEME", const=True, nargs="?", help="enable ttk, with an optional theme")
return parser.parse_args()

# function to check on mode
def __checkMode(self):
Expand Down
19 changes: 13 additions & 6 deletions docs/mkdocs/docs/pythonCommandLine.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@ These are values that can be set when appJar is initially run.
* `--version` `-v`
This will display the version of appJar, then exit.

* `--language` `-l`
* `-l [filename.ini]`
This allows you to set the starting language, only useful if using [internationalisation](/pythoninternationalisation/).
It will override any language configured in the `.go()` function call.

* `-c`, `-e`, `-w`, `-i`, `-d`
This sets the [logging level](/pythonLogging/):
* `-c` - only log CRIRICAL messages
* `-e` - log ERROR messages and above
* `-w` - log WARNING messages and above
* `-i` - log INFO messages and above
* `-d` - log DEBUG messages and above
* `-c` only log CRITICAL messages
* `-e` log ERROR messages and above
* `-w` log WARNING messages and above
* `-i` log INFO messages and above
* `-d` log DEBUG messages and above

* `-f [filename.log]`
This allows you to specify a [file](/pythonLogging/#logging-to-file) to log messages to.

* `--ttk`
This allows you to request appJar uses [ttk widgets](/pythonTtk/) where possible.
It can followed by an optional theme name, to declare which style to use for ttk widgets.

### Example

Expand Down
29 changes: 29 additions & 0 deletions docs/mkdocs/docs/pythonTtk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ttk

appJar includes experimental support for *ttk*, a tk themed widget set.
This is very **BETA.*, and most stuff will break!

## Setup
---

* `.usettk()`
Calling this at the start of your program will tell it to use ttk widgets.

* `.setTtkTheme(theme)`
This allows you to choose which theme to use.

## Command Line Arguments
---

It's possible to switch on ttk theming from the [command line](/pythonCommandLine).
Simply use the `--ttk` flag, with an optional theme name:

```sh
python3 themes.py --ttk # turn on ttk widgets
```

It's also possible to set the file name to log to:

```sh
python3 themes.py --ttk aqua # turn on ttk with the aqua theme
```
5 changes: 4 additions & 1 deletion docs/mkdocs/docs/whatsNew.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
---

## Version 0.08
* [Command line arguments](/pythonCommandLine): appJar now supports command line arguments for [logging](/pythonLogging) and [internatioanlisation](/pythonInternationalisation/) as well as help & version information.
* [Command line arguments](/pythonCommandLine): appJar now supports command line arguments for [logging](/pythonLogging) and [internationalisation](/pythonInternationalisation/) as well as help & version information.

* Issues Resolved:
* [#217](https://github.com/jarvisteach/appJar/issues/217) - Fixed issues with removing [BG images](/pythonImages/#set-background-images)
* [#213](https://github.com/jarvisteach/appJar/issues/213) - Validation entries improved in [Internationalisation](/pythonInternationalisation)
* [#212](https://github.com/jarvisteach/appJar/issues/212) - Creating an empty [Tooltip](/pythonDialogs/#tooltips) now doesn't create anything
* [#211](https://github.com/jarvisteach/appJar/issues/211) - [Logging](/pythonLogging) now logs line number & function name
* [#209](https://github.com/jarvisteach/appJar/issues/209) - Additional testing for [ScrollPanes](/pythonWidgetGrouping/#scroll-pane) & [Grids](/pythonDevWidgets/#grid)
* [#207](https://github.com/jarvisteach/appJar/issues/207) - Now possible to change the title and anchor of a [LabelFrame](/pythonWidgetGrouping/#label-frame)
* [#205](https://github.com/jarvisteach/appJar/issues/205) - Now possible to [select](/pythonWidgets/#set-optionboxes) a disabled item in an OptionBox.
* [#204](https://github.com/jarvisteach/appJar/issues/204) - appJar now supports [Command line arguments](/pythonCommandLine)
* [#203](https://github.com/jarvisteach/appJar/issues/203) - updated docs on [LabelFrames](/pythonWidgetGrouping/#label-frame)
* [#202](https://github.com/jarvisteach/appJar/issues/202) - functions now provided to get or clear all values of a selected widget type
* [#200](https://github.com/jarvisteach/appJar/issues/200) - Switched [Grid](/pythonDevWidgets/#grid) to use a [ScrollPane](/pythonWidgetGrouping/#scroll-pane) & changed ScrollPane to use AutoScrollbars.
* [#189](https://github.com/jarvisteach/appJar/issues/189) - More work on [ttk](/pythonTtk)
* [#177](https://github.com/jarvisteach/appJar/issues/177) - [setLocation](/pythonGuiOptions/#size-location) can now position windows in the center of the screen
* [#162](https://github.com/jarvisteach/appJar/issues/162) - PhotoImage objects can now be passed directly when [adding/setting ImageData](/pythonImages/#add-images)
* [#71](https://github.com/jarvisteach/appJar/issues/71) - More work on [Internationalisation](/pythonInternationalisation/) - now supports LabelFrames, ToggleFrames, TabbedFrames, Properties, SubWindows, SplashScreens & Titles
Expand Down
1 change: 1 addition & 0 deletions docs/mkdocs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pages:
- Internationalisation: pythonInternationalisation.md
- Logging: pythonLogging.md
- Command Line Arguments: pythonCommandLine.md
- ttk: pythonTtk.md
- Examples:
- Raspberry Pi Minecraft: examples/minecraft.md
- Simple Layout: examples/simple.md
Expand Down

0 comments on commit c296d2e

Please sign in to comment.