mirror of
https://github.com/Instagram/LibCST.git
synced 2025-12-23 10:35:53 +00:00
Implement a --python-version override flag to "libcst.tool print".
This commit is contained in:
parent
bcae954484
commit
d30b04559d
1 changed files with 20 additions and 2 deletions
|
|
@ -23,7 +23,7 @@ from typing import Any, Callable, Dict, List, Sequence, Tuple, Type
|
|||
|
||||
import yaml
|
||||
|
||||
from libcst import CSTNode, IndentedBlock, Module, parse_module
|
||||
from libcst import CSTNode, IndentedBlock, Module, PartialParserConfig, parse_module
|
||||
from libcst._nodes.deep_equals import deep_equals
|
||||
from libcst.codemod import (
|
||||
CodemodCommand,
|
||||
|
|
@ -242,6 +242,17 @@ def _print_tree_impl(proc_name: str, command_args: List[str]) -> int:
|
|||
action="store_true",
|
||||
help="Show values that exist only for syntax, like commas or semicolons",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-p",
|
||||
"--python-version",
|
||||
metavar="VERSION",
|
||||
help=(
|
||||
"Override the version string used for parsing Python source files. Defaults "
|
||||
+ "to the version of python used to run this tool."
|
||||
),
|
||||
type=str,
|
||||
default=None,
|
||||
)
|
||||
args = parser.parse_args(command_args)
|
||||
infile = args.infile
|
||||
|
||||
|
|
@ -252,7 +263,14 @@ def _print_tree_impl(proc_name: str, command_args: List[str]) -> int:
|
|||
with open(infile, "rb") as fp:
|
||||
code = fp.read()
|
||||
|
||||
tree = parse_module(code)
|
||||
tree = parse_module(
|
||||
code,
|
||||
config=(
|
||||
PartialParserConfig(python_version=args.python_version)
|
||||
if args.python_version is not None
|
||||
else PartialParserConfig()
|
||||
),
|
||||
)
|
||||
print(
|
||||
dump(
|
||||
tree,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue