~dricottone/filters

ref: 1a1eaedc0203a3bb86b1ff5d41a4d8540930a4b0 filters/test.py -rwxr-xr-x 1.5 KiB
1a1eaedc — dricottone Initial commit 5 years ago
                                                                                
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
50
51
52
#!/usr/bin/env python3.7

# custom lib
from mylib.env import ENV, read_stdin
from mylib.cli import ARGS
from mylib.fs import File, exists

# typing
from typing import *


def read_args_or_stdin(*,
                       arg_number: int = 1,
                       opts: Tuple[str, ...] = ('f', 'files'),
                       opt_number: int = 1                    ) -> List[str]:
    """
    Input handler for well-behaved scripts.

    Checks for '-f' or '--file' command line options and N file names following
    them. Else check for the N first positional argument. Else assume STDIN. If
    '-' is found at any step, STDIN is used in place.

    Options:
      arg_number : Max num. of files from positional arguments  [Default: 1]
      opts       : File option flags [Default: 'f', 'file']
      opt_number : Max num. of files from file options [Default: 1]
    """
    # process cli
    fnames = ARGS.getany(opts, [], number=opt_number)
    positional = ARGS.positional()
    if not fnames and positional:
        fnames = positional[:arg_number]

    # build buffer
    if fnames:
        buffer = list()
        for fname in fnames:
            if fname == '-':
                buffer += read_stdin() # env.read_stdin
            elif exists(fname): # fs.exists
                buffer += File(fname).lines()
            else:
                pass # TODO: option for raising here
    else:
        buffer = read_stdin() # env.read_stdin

    return buffer

if __name__ == '__main__':
    lines = read_args_or_stdin()
    print('\n'.join(lines))