-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcli_utils.py
More file actions
34 lines (29 loc) · 1.11 KB
/
cli_utils.py
File metadata and controls
34 lines (29 loc) · 1.11 KB
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
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/aboutcode-org/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import os
from itertools import chain
import click
class FileOptionType(click.File):
"""
A click.File subclass that ensures that a file name is not set to an
existing option parameter to avoid mistakes.
"""
def convert(self, value, param, ctx):
known_opts = set(
chain.from_iterable(p.opts for p in ctx.command.params if isinstance(p, click.Option))
)
if value in known_opts:
self.fail(
"Illegal file name conflicting with an option name: "
f"{os.fsdecode(value)}. "
'Use the special "-" file name to print results on screen/stdout.',
param,
ctx,
)
return click.File.convert(self, value, param, ctx)