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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use crate::common::{Handler, MimeOrExtension, UserPath};
use clap::Parser;

/// A better xdg-utils
///
/// Resource opener with support for wildcards, multiple handlers, and regular expressions.
///
/// Based on handlr at <https://github.com/chmln/handlr>
///
/// Regular expression handlers inspired by mimeo at <https://xyne.dev/projects/mimeo/>
#[deny(missing_docs)]
#[derive(Parser)]
#[clap(global_setting = clap::AppSettings::DeriveDisplayOrder)]
#[clap(disable_help_subcommand = true)]
#[clap(version, about)]
pub enum Cmd {
    /// List default apps and the associated handlers
    ///
    /// Output is formatted as a table with two columns.
    /// The left column shows mimetypes and the right column shows the handlers
    ///
    /// Currently does not support regex handlers.
    List {
        #[clap(long, short)]
        /// Expand wildcards in mimetypes and show global defaults
        all: bool,
    },

    /// Open a path/URL with its default handler
    ///
    /// Unlike xdg-open and similar resource openers, multiple paths/URLs may be supplied.
    ///
    /// If multiple handlers are set and `enable_selector` is set to true,
    /// you will be prompted to select one using `selector` from ~/.config/handlr/handlr.toml.
    /// Otherwise, the default handler will be opened.
    Open {
        #[clap(required = true)]
        /// Paths/URLs to open
        paths: Vec<UserPath>,
    },

    /// Set the default handler for mime/extension
    ///
    /// Overwrites currently set handler(s) for the given mime/extension.
    ///
    /// Asterisks can be used as wildcards to set multiple mimetypes.
    ///
    /// File extensions are converted into their respective mimetypes in mimeapps.list.
    ///
    /// Currently does not support regex handlers.
    Set {
        /// Mimetype or file extension to operate on.
        mime: MimeOrExtension,
        /// Desktop file of handler program
        handler: Handler,
    },

    /// Unset the default handler for mime/extension
    ///
    /// Wildcards cannot be used unless removing handlers that already have wildcards.
    ///
    /// If multiple default handlers are set, both will be removed.
    ///
    /// Currently does not support regex handlers.
    Unset {
        /// Mimetype or file extension to unset the default handler of
        mime: MimeOrExtension,
    },

    /// Launch the handler for specified extension/mime with optional arguments
    ///
    /// Only supports wildcards for mimetypes for handlers that have been set or added with wildcards.
    ///
    /// If multiple handlers are set and `enable_selector` is set to true,
    /// you will be prompted to select one using `selector` from ~/.config/handlr/handlr.toml.
    /// Otherwise, the default handler will be opened.
    Launch {
        /// Mimetype or file extension to launch the handler of
        mime: MimeOrExtension,
        /// Arguments to pass to handler program
        args: Vec<UserPath>,
    },

    /// Get handler for this mime/extension
    ///
    /// If multiple handlers are set and `enable_selector` is set to true,
    /// you will be prompted to select one using `selector` from ~/.config/handlr/handlr.toml.
    /// Otherwise, only the default handler will be printed.
    ///
    /// Note that regex handlers are not supported by this subcommand currently.
    ///
    /// When using `--json`, output is in the form:
    ///
    /// ```json
    ///
    /// {
    ///
    ///   "handler": "helix.desktop",
    ///
    ///   "name": "Helix",
    ///
    ///   "cmd": "helix"
    ///
    /// }
    ///
    /// ```
    ///
    /// Note that when handlr is not being directly output to a terminal, and the handler is a terminal program,
    /// the "cmd" key in the json output will include the command of the `x-scheme-handler/terminal` handler.
    Get {
        #[clap(long)]
        /// Output handler info as json
        json: bool,
        /// Mimetype to get the handler of
        mime: MimeOrExtension,
    },

    /// Add a handler for given mime/extension
    ///
    /// Note that the first handler is the default.
    ///
    /// This subcommand adds secondary handlers that coexist with the default
    /// and does not overwrite existing handlers.
    Add {
        /// Mimetype to add handler to
        mime: MimeOrExtension,
        /// Desktop file of handler program
        handler: Handler,
    },

    /// Get the mimetype of a given file/URL
    ///
    /// By default, output is in the form of a table that matches file paths/URLs to their mimetypes.
    ///
    /// When using `--json`, output will be in the form:
    ///
    /// ```json
    ///
    /// [
    ///
    ///   {
    ///
    ///     "path": "README.md"
    ///
    ///     "mime": "text/markdown"
    ///
    ///   },
    ///
    ///   {
    ///
    ///     "path": "https://duckduckgo.com/"
    ///
    ///     "mime": "x-scheme-handler/https"
    ///
    ///   }
    ///
    /// ...
    ///
    /// ]
    ///
    /// ```
    Mime {
        #[clap(long)]
        /// Output mimetype info as json
        json: bool,
        /// File paths/URLs to get the mimetype of
        paths: Vec<UserPath>,
    },

    #[clap(hide = true)]
    /// Helper subcommand for autocompletion scripts; should be hidden
    ///
    /// This should not be visible in `handlr --help` output, autocompletion, or man pages.
    /// If you see this there, please open an issue on Github.
    ///
    /// However it is fine if this shows up in the output of `handlr autocomplete --help`.
    Autocomplete {
        #[clap(short)]
        /// Autocomplete for desktop files present on system
        desktop_files: bool,
        #[clap(short)]
        /// Autocomplete for mimetypes/file extensions
        mimes: bool,
    },
}