[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Fab-user] Have you considered using the plac syntax for command line ar
From: |
Matthew Honnibal |
Subject: |
[Fab-user] Have you considered using the plac syntax for command line arguments? |
Date: |
Mon, 25 Feb 2013 01:41:00 +1100 |
I noticed this in the "fab usage and options" docs:
"Additionally, since this process involves string parsing, all values
will end up as Python strings, so plan accordingly. (We hope to
improve upon this in future versions of Fabric, provided an intuitive
syntax can be found.)"
When I started using fabric, one thing I noticed was that I missed the
interfaces I was getting from the plac library:
http://plac.googlecode.com/hg/doc/plac.html
An example from the docs:
@plac.annotations(
db=plac.Annotation("Connection string", type=SqlSoup),
header=plac.Annotation("Header", 'flag', 'H'),
sqlcmd=plac.Annotation("SQL command", 'option', 'c', str, metavar="SQL"),
delimiter=plac.Annotation("Column separator", 'option', 'd'),
scripts=plac.Annotation("SQL scripts"),
)
def main(db, header, sqlcmd, delimiter="|", *scripts):
"A script to run queries and SQL scripts on a database"
yield 'Working on %s' % db.bind.url
if sqlcmd:
result = db.bind.execute(sqlcmd)
if header: # print the header
print delimiter.join(result.keys())
for row in result: # print the rows
print delimiter.join(map(str, row))
for script in scripts:
db.bind.execute(open(script).read())
print 'executed %s' % script
if __name__ == '__main__':
plac.call(main)
If run with no or invalid arguments, it automatically generates the
help message:
usage: dbcli.py [-h] [-H] [-c SQL] [-d |] db [scripts [scripts ...]]
A script to run queries and SQL scripts on a database
positional arguments:
db Connection string
scripts SQL scripts
optional arguments:
-h, --help show this help message and exit
-H, --header Header
-c SQL, --sqlcmd SQL SQL command
-d |, --delimiter | Column separator
Now, there's any number of reasons why this might not be the right
approach for fabric, or why other people might think it's actually
awful. But I find it very nice, so I just wanted to check you were
aware of it as a potential solution.
- [Fab-user] Have you considered using the plac syntax for command line arguments?,
Matthew Honnibal <=