Skip to content
Browse files

Modified phone to use the optparse module to provide a more traditional

command line argument scheme.  Also, updated the COOKBOOK and README
files to reflect these changes.
  • Loading branch information...
1 parent 6adcb26 commit 2319716feacd24783a0acc284fe0c011d4ec573f @pbutler pbutler committed with
Showing with 39 additions and 31 deletions.
  1. +2 −2 COOKBOOK.md
  2. +7 −4 README.md
  3. +30 −25 phone
View
4 COOKBOOK.md
@@ -21,7 +21,7 @@ Rtorrent (0.8.4+) notification on download complete
Add this line to ~/.rtorrent.rc:
- system.method.set_key = event.download.finished,notify_me,"execute=/usr/local/bin/phone,---title=rtorrent,$d.get_name="
+ system.method.set_key = event.download.finished,notify_me,"execute=/usr/local/bin/phone,--title=rtorrent,--,$d.get_name="
Notify when user logs in on Mac OSX 10.3+
-----------------------------------------
@@ -29,7 +29,7 @@ Notify when user logs in on Mac OSX 10.3+
Stick this in /Library/Scripts/login.sh:
#!/bin/bash
- /usr/local/bin/phone ---username=<username> ---secret=<secret> ---title="`hostname` login" $1
+ /usr/local/bin/phone --username=<username> --secret=<secret> --title="`hostname` login" $1
Then run this:
View
11 README.md
@@ -29,14 +29,17 @@ The first time you run Phone Pipe it will prompt you for your credentials. It
will store these in ~/.phonepipe for future use. Alternatively you can also
specify your username and API key on the command line like such:
- $ phone ---user=someuser ---secret=b52e5fd8b6f14d799798172c1b62c7eb
+ $ phone --user=someuser --secret=b52e5fd8b6f14d799798172c1b62c7eb
Usage
-----
-All options are triple hyphen prefixed to avoid being squashed by cut and
-paste output or other bits that might be sent in messages.
+phone [options] [--] [msg]
+
+The options are described below. If you are concerned about the message given
+on the command line containing dashes and therefore looking like arguments, you
+may include a double dash before the message give on the command line.
---title - The title of the message.
---label - A message label, generally the name of the sending service.
@@ -56,7 +59,7 @@ Examples
--------
# First message
- $ phon first message
+ $ phone first message
# Notify yourself when the compile is done
$ make; phone
View
55 phone
@@ -9,8 +9,11 @@
If you do find a use for it, please consider submitting patches to
http://github.com/drewcrawford/Phone-Pipe or just shooting me an
e-mail at drew@sealedabstract.com
-"""
+
+Usage: %prog [options] [--] [msg]"""
+
+import optparse
import getpass
import os
import sys
@@ -26,14 +29,7 @@ except:
API_URL = "https://%(username)s:%(secret)s@api.notifo.com/v1/send_notification"
SETTINGS = os.path.expanduser("~/.phonepipe")
-def get_credentials():
- username = None
- secret = None
- for arg in sys.argv:
- if arg.startswith("---username="):
- username = arg[12:]
- elif arg.startswith("---secret="):
- secret = arg[10:]
+def get_credentials(username = None, secret = None):
if username is not None and secret is not None:
return {"username": user, "secret": secret}
if not os.path.exists(SETTINGS):
@@ -71,9 +67,9 @@ def run_file(file):
else:
os.startfile(file)
-def alert(msg, url=None, title="phonepipe", label="phonepipe"):
+def alert(msg, url=None, title="phonepipe", label="phonepipe", username=None, secret = None):
msg = msg.strip()
- url = API_URL % get_credentials()
+ url = API_URL % get_credentials(username, secret)
data = urllib.urlencode({
"title": title,
"msg": msg,
@@ -101,19 +97,28 @@ def alert(msg, url=None, title="phonepipe", label="phonepipe"):
def main():
msg = []
- url = None
- title = "phonepipe"
- label = "phonepipe"
- for arg in sys.argv[1:]:
- if arg.startswith("---url="):
- url = arg[7:]
- elif arg.startswith("---title="):
- title = arg[9:]
- elif arg.startswith("---label="):
- label = arg[9:]
- else:
- msg.append(arg)
- msg = [" ".join(msg)]
+
+ parser = optparse.OptionParser()
+ parser.usage = __doc__
+ parser.add_option("-u", "--username",
+ action="store", dest="username", default=None,
+ help="Override your ~/.phonepipe if you specify a secret as well.")
+ parser.add_option("-s", "--secret",
+ action="store", dest="secret", default=None,
+ help="Override your ~/.phonepipe when specified with a username.")
+ parser.add_option("-t", "--title",
+ action="store", dest="title", type="str", default="phonepipe",
+ help="The title of the message. (default: 'phonepipe')")
+ parser.add_option("-l", "--label",
+ action="store", dest="label", type="str", default="phonepipe",
+ help="A message label, generally the name of the sending service. (default: 'phonepipe')")
+ parser.add_option("", "--url",
+ action="store", dest="url", default=None,
+ help="If specified, the Notifo app makes a link from the message to it.")
+ (options, args) = parser.parse_args()
+ if len(args) > 0:
+ msg = [ " ".join(args) ]
+
if not sys.stdin.isatty():
try:
for line in sys.stdin.readlines():
@@ -123,7 +128,7 @@ def main():
msg = "".join(msg)
if not msg:
msg = "Phone Pipe Alert"
- alert(msg, url=url, title=title, label=label)
+ alert(msg, url=options.url, title=options.title, label=options.label, username=options.username, secret=options.secret)
if __name__ == '__main__':
main()

0 comments on commit 2319716

Please sign in to comment.
Something went wrong with that request. Please try again.