OILS / tools / readlink.py View on Github | oilshell.org

31 lines, 21 significant
1from __future__ import print_function
2"""
3readlink.py - Minimal implementation of readlink -f, e.g. for OS X.
4"""
5
6import libc
7from frontend import args
8from frontend import flag_spec
9from mycpp.mylib import print_stderr
10
11from typing import List
12
13SPEC = flag_spec.FlagSpec('readlink')
14SPEC.ShortFlag('-f')
15
16
17def main(argv):
18 # type: (List[str]) -> int
19 arg_r = args.Reader(argv)
20 arg_r.Next() # skip argv[0]
21 arg = args.Parse(SPEC, arg_r)
22
23 if not arg.f:
24 print_stderr("readlink: -f must be passed")
25 return 1
26 for path in arg_r.Rest():
27 res = libc.realpath(path)
28 if res is None:
29 return 1
30 print(res)
31 return 0