Change the command-line interface. Don't use optparse just use sys.argv:

makelinks link|tidy|report [FROM_DIR [TO_DIR]]
This commit is contained in:
Sean Hammond 2010-02-08 09:38:19 -05:00
parent 3af41d5a93
commit e2fce1a2fb
1 changed files with 29 additions and 28 deletions

View File

@ -216,47 +216,48 @@ def link(from_dir,to_dir,report=False):
os.symlink(to_path,from_path) os.symlink(to_path,from_path)
def usage(): def usage():
return """makelinks [options] link|tidy|report return """Usage:
makelinks link|tidy|report [FROM_DIR [TO_DIR]]
Commands: Commands:
link -- make symlinks in FROM_DIR to files and directories in TO_DIR link -- make symlinks in FROM_DIR to files and directories in TO_DIR
tidy -- remove broken symlinks from FROM_DIR tidy -- remove broken symlinks from FROM_DIR
report -- report on symlinks in FROM_DIR and files and directories in TO_DIR""" report -- report on symlinks in FROM_DIR and files and directories in TO_DIR
FROM_DIR defaults to ~ and TO_DIR defaults to ~/.dotfiles.
"""
if __name__ == "__main__": if __name__ == "__main__":
import optparse try:
parser = optparse.OptionParser(usage=usage()) ACTION = sys.argv[1]
except IndexError:
parser.add_option('-f', '--from', action='store', dest='FROM_DIR',default=os.path.expanduser('~'), help="The directory to create symlinks in.") print usage()
parser.add_option('-t', '--to', action='store', dest='TO_DIR',default=os.path.join(os.path.expanduser('~'),'.dotfiles'), help="The directory to create symlinks to.") sys.exit(2)
try:
options, remainder = parser.parse_args() FROM_DIR = sys.argv[2]
except IndexError:
FROM_DIR = options.FROM_DIR FROM_DIR = os.path.expanduser('~')
if not os.path.isdir(FROM_DIR): if not os.path.isdir(FROM_DIR):
print FROM_DIR+" is not a directory!" print "FROM_DIR %s is not a directory!" % FROM_DIR
parser.print_usage() print usage()
sys.exit(2) sys.exit(2)
try:
TO_DIR = options.TO_DIR TO_DIR = sys.argv[3]
except IndexError:
TO_DIR = os.path.join(os.path.expanduser('~'),'.dotfiles')
if not os.path.isdir(TO_DIR): if not os.path.isdir(TO_DIR):
print TO_DIR+" is not a directory!" print "TO_DIR %s is not a directory!" % TO_DIR
parser.print_usage() print usage()
sys.exit(2) sys.exit(2)
if len(remainder) != 1: if ACTION == 'link':
parser.print_usage()
sys.exit(2)
else:
COMMAND = remainder[0]
if COMMAND == 'link':
link(FROM_DIR,TO_DIR) link(FROM_DIR,TO_DIR)
elif COMMAND == 'tidy': elif ACTION == 'tidy':
tidy(FROM_DIR) tidy(FROM_DIR)
elif COMMAND == 'report': elif ACTION == 'report':
link(FROM_DIR,TO_DIR,report=True) link(FROM_DIR,TO_DIR,report=True)
tidy(FROM_DIR,report=True) tidy(FROM_DIR,report=True)
else: else:
parser.print_usage() print usage()
sys.exit(2) sys.exit(2)