Re: Is it ‘allowed’ to get parameters like this
Lawrence D’Oliveiro
lawrencedo99 at gmail.com
Thu Aug 11 22:55:34 EDT 2016
On Friday, August 12, 2016 at 1:28:04 AM UTC+12, Cecil Westerhof wrote:
> As I understood it getopt is the way to get the parameters for your
> script. But at the moment it looks a bit like overkill, my script does
> not have much parameters as parameter_error shows:
That’s fine. But your command-line needs are so simple, I wouldn’t bother with the “--” business either. Just take the interval as a single argument. Here’s how I would do it:
if len(sys.argv) == 2 :
periodspec = sys.argv[1]
elif len(sys.argv) == 1 :
periodspec = "all"
else :
raise RuntimeError("usage:\n {} [period]" % sys.argv[0])
#end if
today = time.strftime("%Y-%m-%d") # or whatever
# original did “select current_date” from SQLite
if re.fullmatch(r"^\d+$", periodspec) != None :
period = int(periodspec)
else :
period = \
{
"today" : lambda : today + "%",
"this-month" : lambda : today[0:8] + "%",
"this-year" : lambda : today[0:5] + "%",
"all" : lambda : "%",
}.get(periodspec, lambda : None)()
if period == None :
raise RuntimeError("{}: unrecognized period spec “%s”" % periodspec)
#end if
#end if
sys.stderr.write("period = %s\n" % repr(period)) # debug
# ...
# vmstat_params = (period, slice_length)
# ... etc
So you would call that like
./demo 5
./demo today
./demo
etc. And as a bonus, it will accept any integer interval, not just your original limited set. And you see how I wrote the table of symbolic intervals? That makes it easy to add new entries.
More information about the Python-list
mailing list