-
Notifications
You must be signed in to change notification settings - Fork 0
/
relpath
executable file
·49 lines (39 loc) · 1.21 KB
/
relpath
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
##############################################################################
#
# NAME: relpath
#
# DESCRIPTION: Prints a given path relative to another directory
#
# USAGE: relpath FILE [DIR]
#
# DEPENDENCIES: None
#
##############################################################################
#
__description__ = 'Prints a given path relative to another directory'
# ---------------------------------------------------------------------------
# Standard imports:
import sys
import argparse
import os
# ---------------------------------------------------------------------------
def parse_args(argv):
""" Parse and validate command line arguments """
parser = argparse.ArgumentParser(description=__description__)
parser.add_argument('path')
parser.add_argument('base', nargs='?', default='.')
args = parser.parse_args(argv[1:])
return args
def main(argv=None):
""" Run this program """
if argv is None:
argv = [__name__]
args = parse_args(argv)
try:
print(os.path.relpath(args.path, args.base))
except KeyboardInterrupt:
sys.exit(-1)
if __name__ == '__main__':
sys.exit(main(sys.argv) or 0)