ffmt_cli Module

Command-line interface: argument parsing and processing of files and directories.


Uses

  • module~~ffmt_cli~~UsesGraph module~ffmt_cli ffmt_cli iso_fortran_env iso_fortran_env module~ffmt_cli->iso_fortran_env module~ffmt_config ffmt_config module~ffmt_cli->module~ffmt_config module~ffmt_diff ffmt_diff module~ffmt_cli->module~ffmt_diff module~ffmt_format ffmt_format module~ffmt_cli->module~ffmt_format module~ffmt_fs ffmt_fs module~ffmt_cli->module~ffmt_fs module~ffmt_io ffmt_io module~ffmt_cli->module~ffmt_io module~ffmt_options ffmt_options module~ffmt_cli->module~ffmt_options module~ffmt_settings ffmt_settings module~ffmt_cli->module~ffmt_settings module~ffmt_strings ffmt_strings module~ffmt_cli->module~ffmt_strings module~ffmt_walk ffmt_walk module~ffmt_cli->module~ffmt_walk module~ffmt_config->module~ffmt_fs module~ffmt_config->module~ffmt_settings module~ffmt_config->module~ffmt_strings tomlf tomlf module~ffmt_config->tomlf tomlf_constants tomlf_constants module~ffmt_config->tomlf_constants module~ffmt_diff->iso_fortran_env module~ffmt_diff->module~ffmt_strings module~ffmt_format->module~ffmt_options module~ffmt_format->module~ffmt_strings module~ffmt_case ffmt_case module~ffmt_format->module~ffmt_case module~ffmt_layout ffmt_layout module~ffmt_format->module~ffmt_layout module~ffmt_lexer ffmt_lexer module~ffmt_format->module~ffmt_lexer module~ffmt_linebreak ffmt_linebreak module~ffmt_format->module~ffmt_linebreak module~ffmt_relational ffmt_relational module~ffmt_format->module~ffmt_relational module~ffmt_source ffmt_source module~ffmt_format->module~ffmt_source module~ffmt_statements ffmt_statements module~ffmt_format->module~ffmt_statements module~ffmt_structure ffmt_structure module~ffmt_format->module~ffmt_structure module~ffmt_tokens ffmt_tokens module~ffmt_format->module~ffmt_tokens module~ffmt_use_sort ffmt_use_sort module~ffmt_format->module~ffmt_use_sort module~ffmt_whitespace ffmt_whitespace module~ffmt_format->module~ffmt_whitespace iso_c_binding iso_c_binding module~ffmt_fs->iso_c_binding module~ffmt_settings->module~ffmt_options module~ffmt_settings->module~ffmt_strings module~ffmt_walk->module~ffmt_fs module~ffmt_walk->module~ffmt_strings fregex fregex module~ffmt_walk->fregex module~ffmt_case->module~ffmt_options module~ffmt_case->module~ffmt_source module~ffmt_case->module~ffmt_statements module~ffmt_case->module~ffmt_tokens module~ffmt_layout->module~ffmt_options module~ffmt_layout->module~ffmt_strings module~ffmt_layout->module~ffmt_case module~ffmt_layout->module~ffmt_source module~ffmt_layout->module~ffmt_statements module~ffmt_layout->module~ffmt_structure module~ffmt_layout->module~ffmt_tokens module~ffmt_layout->module~ffmt_whitespace module~ffmt_lexer->module~ffmt_strings module~ffmt_lexer->module~ffmt_tokens module~ffmt_linebreak->module~ffmt_strings module~ffmt_linebreak->module~ffmt_source module~ffmt_linebreak->module~ffmt_statements module~ffmt_linebreak->module~ffmt_tokens module~ffmt_relational->module~ffmt_options module~ffmt_relational->module~ffmt_strings module~ffmt_relational->module~ffmt_source module~ffmt_relational->module~ffmt_statements module~ffmt_relational->module~ffmt_tokens module~ffmt_source->module~ffmt_lexer module~ffmt_source->module~ffmt_statements module~ffmt_source->module~ffmt_tokens module~ffmt_directives ffmt_directives module~ffmt_source->module~ffmt_directives module~ffmt_statements->module~ffmt_strings module~ffmt_statements->module~ffmt_tokens module~ffmt_structure->module~ffmt_strings module~ffmt_structure->module~ffmt_source module~ffmt_structure->module~ffmt_statements module~ffmt_structure->module~ffmt_tokens module~ffmt_constructs ffmt_constructs module~ffmt_structure->module~ffmt_constructs module~ffmt_tokens->module~ffmt_strings module~ffmt_use_sort->module~ffmt_strings module~ffmt_use_sort->module~ffmt_source module~ffmt_use_sort->module~ffmt_statements module~ffmt_use_sort->module~ffmt_tokens module~ffmt_whitespace->module~ffmt_options module~ffmt_whitespace->module~ffmt_strings module~ffmt_whitespace->module~ffmt_source module~ffmt_whitespace->module~ffmt_statements module~ffmt_whitespace->module~ffmt_tokens module~ffmt_constructs->module~ffmt_strings module~ffmt_constructs->module~ffmt_statements module~ffmt_constructs->module~ffmt_tokens module~ffmt_directives->module~ffmt_statements module~ffmt_directives->module~ffmt_tokens

Variables

Type Visibility Attributes Name Initial
integer, public, parameter :: EXIT_CHANGED = 1

with --check: some files are not formatted

integer, public, parameter :: EXIT_ERROR = 2

bad arguments, unreadable files or internal errors

integer, public, parameter :: EXIT_OK = 0

success

character(len=*), public, parameter :: VERSION = '1.0.3'

Derived Types

type, public ::  cli_t

Parsed command line.

Components

Type Visibility Attributes Name Initial
logical, public :: check = .false.

report files that would change; write nothing

character(len=:), public, allocatable :: config

--config file, or ''

logical, public :: diff = .false.

write a diff of the changes; write nothing else

type(string_t), public, allocatable :: exclude(:)

--exclude patterns, added to the configured ones

type(string_t), public, allocatable :: extensions(:)

--extensions; not allocated if not given

logical, public :: help = .false.

show the --help text and do nothing else

logical, public :: no_config = .false.

ignore configuration files

type(string_t), public, allocatable :: paths(:)

files, directories, or - for standard input

logical, public :: quiet = .false.

do not report warnings

type(setting_t), public, allocatable :: settings(:)

formatting settings given on the command line, in order

logical, public :: stdout = .false.

write results to standard output

logical, public :: version = .false.

show the version and do nothing else

type, private ::  config_cache_t

Configurations already found, by directory.

Components

Type Visibility Attributes Name Initial
type(config_t), public, allocatable :: configs(:)

the configuration that applies in each of dirs

type(string_t), public, allocatable :: dirs(:)

absolute paths of the directories looked up


Functions

private pure function ends_with(s, suffix)

True if string s ends with suffix.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: s

the string, and the ending to look for

character(len=*), intent(in) :: suffix

the string, and the ending to look for

Return Value logical

private function process_file(cli, cache, path) result(status)

Format file path in place (or write it to standard output, or report it with --check / --diff), with the configuration of its directory, and return the exit status.

Arguments

Type IntentOptional Attributes Name
type(cli_t), intent(in) :: cli

the parsed command line

type(config_cache_t), intent(inout) :: cache

configurations found so far

character(len=*), intent(in) :: path

the file, as given on the command line or found in a directory

Return Value integer

private function process_stdin(cli, cache) result(status)

Format standard input to standard output (or report it with --check / --diff), with the configuration of the current directory, and return the exit status.

Arguments

Type IntentOptional Attributes Name
type(cli_t), intent(in) :: cli

the parsed command line

type(config_cache_t), intent(inout) :: cache

configurations found so far

Return Value integer

private function process_text(cli, cache, name, dir, text, to_stdout) result(status)

Format text from file name in directory dir, and write or report the result.

Arguments

Type IntentOptional Attributes Name
type(cli_t), intent(in) :: cli

the parsed command line

type(config_cache_t), intent(inout) :: cache

configurations found so far

character(len=*), intent(in) :: name

the file, for messages and diff headers and to write the result to; <stdin> for standard input

character(len=*), intent(in) :: dir

the directory whose configuration applies

character(len=*), intent(in) :: text

the source as read

logical, intent(in) :: to_stdout

write the result to standard output instead of to name

Return Value integer

public function run(cli) result(status)

Format the paths of cli and return the exit status.

Arguments

Type IntentOptional Attributes Name
type(cli_t), intent(in) :: cli

the parsed command line

Return Value integer

public function usage() result(text)

The --help text.

Arguments

None

Return Value character(len=:), allocatable


Subroutines

private subroutine config_for(cli, cache, dir, cfg, stat, errmsg)

The configuration that applies in directory dir (absolute).

Arguments

Type IntentOptional Attributes Name
type(cli_t), intent(in) :: cli

the parsed command line (--config, --no-config)

type(config_cache_t), intent(inout) :: cache

configurations found so far; cfg is added

character(len=*), intent(in) :: dir

absolute path of the directory

type(config_t), intent(out) :: cfg

the configuration; cfg%path is '' if there is none

integer, intent(out) :: stat

nonzero if a configuration file cannot be read

character(len=:), intent(out), allocatable :: errmsg

what is wrong, if stat is nonzero

private subroutine directory_files(cli, cache, dir, files, status)

The Fortran files under dir, selected with the [files] settings of its configuration and the command line.

Arguments

Type IntentOptional Attributes Name
type(cli_t), intent(in) :: cli

the parsed command line

type(config_cache_t), intent(inout) :: cache

configurations found so far

character(len=*), intent(in) :: dir

the directory, as given on the command line

type(string_t), intent(out), allocatable :: files(:)

paths of the files found

integer, intent(inout) :: status

exit status so far; set to EXIT_ERROR if the configuration cannot be read

private subroutine options_for(cli, cache, dir, opts, stat, errmsg)

The options for a file in directory dir: defaults, then configuration, then command line.

Arguments

Type IntentOptional Attributes Name
type(cli_t), intent(in) :: cli

the parsed command line

type(config_cache_t), intent(inout) :: cache

configurations found so far

character(len=*), intent(in) :: dir

the file's directory, relative or absolute

type(format_options_t), intent(out) :: opts

the options

integer, intent(out) :: stat

nonzero for an unreadable configuration or a bad setting

character(len=:), intent(out), allocatable :: errmsg

what is wrong, if stat is nonzero

public subroutine parse_args(args, cli, stat, errmsg)

Parse command-line arguments (without the program name).

Arguments

Type IntentOptional Attributes Name
type(string_t), intent(in) :: args(:)

the arguments, in order

type(cli_t), intent(out) :: cli

the parsed command line

integer, intent(out) :: stat

0, or EXIT_ERROR for a bad command line

character(len=:), intent(out), allocatable :: errmsg

what is wrong, if stat is nonzero