all2md.cli.commands.shared

Shared utilities for all2md CLI commands.

This module provides common functionality used across multiple CLI commands, including input file collection from various sources (filesystem, URIs, stdin), version information, system diagnostics, and batch file list parsing.

all2md.cli.commands.shared.protect_stdout() Iterator[None]

Redirect OS-level stdout (fd 1) to stderr for the duration.

Some libraries in the conversion pipeline (notably PyMuPDF) print advisories straight to stdout, which would corrupt a command’s own output stream – especially a machine-readable --json one. We point fd 1 at stderr while parsing, then restore it before emitting results. This catches both Python-level print and C-level writes. Falls back to a no-op if fd duplication is unavailable (e.g. a captured stdout with no real descriptor).

all2md.cli.commands.shared.add_cache_arguments(parser: ArgumentParser) None

Add the shared opt-in conversion-cache flags to a subcommand parser.

Commands that convert documents (grep, search, chunk, view, serve) share this so the flag names and help text stay consistent.

all2md.cli.commands.shared.conversion_cache_from_args(parsed: Namespace) Any

Return the use_conversion_cache context configured from parsed args.

--cache forces caching on; without it the ALL2MD_CACHE environment variable still applies (enabled=None defers to the env). Safe to call on a namespace lacking the attributes (returns an inert, disabled context).

all2md.cli.commands.shared.split_glob_pattern(raw: str) tuple[Path, str, bool]

Split a glob pattern into (anchor_dir, name_pattern, recursive).

The anchor is the longest leading run of path components that contain no glob metacharacters; it is the concrete directory the pattern is rooted in. name_pattern is the trailing filename component (an fnmatch pattern), and recursive is True when the pattern contains a ** segment.

Examples

subdir/*.docx -> (Path('subdir'), '*.docx', False) subdir/**/*.docx -> (Path('subdir'), '*.docx', True) *.docx -> (Path('.'), '*.docx', False)

all2md.cli.commands.shared.has_hidden_component(path: Path, root: Path) bool

Return True if path has a dot-prefixed component below root.

Only components discovered relative to root are considered, so an explicitly provided hidden root (e.g. .config/) does not by itself mark everything underneath as hidden.

all2md.cli.commands.shared.collect_input_files(input_paths: List[str], recursive: bool = False, extensions: List[str] | None = None, exclude_patterns: List[str] | None = None, include_hidden: bool = False) List[CLIInputItem]

Collect CLI input items from provided arguments.

Supports local filesystem paths, directories, shell globs, URIs, and stdin. Local paths are expanded to files, filtered by extension when requested, and deduplicated. Remote inputs are preserved as strings to maintain compatibility with the document loader infrastructure.

Dot-files and dot-folders discovered while scanning directories or expanding globs are skipped unless include_hidden is True. Explicitly named files are always included.

all2md.cli.commands.shared.get_version() str

Get the version of all2md package.

all2md.cli.commands.shared.get_about_info() str

Get detailed information about all2md including system info and dependencies.

all2md.cli.commands.shared.parse_batch_list(list_path: Path | str) List[str]

Parse batch list file and return file paths.

Parameters:

list_path (Path or str) – Path to the list file containing file paths (one per line), or ‘-’ to read from stdin

Returns:

List of file path strings ready for collect_input_files()

Return type:

List[str]

Raises:

argparse.ArgumentTypeError – If the list file cannot be read or contains invalid entries

Notes

List file format: - One file path per line - Lines starting with # are comments - Blank lines are ignored - File paths are resolved relative to the list file directory (or cwd if stdin) - All paths are validated to exist - Use ‘-’ as the path to read the list from stdin

Examples

List file content:

# Input files for processing chapter1.pdf chapter2.pdf /absolute/path/to/file.docx

Reading from stdin:

$ echo “file1.pdf” | all2md –batch-from-list - –output-dir ./out