CLI reference: group flags and their environment variables
A command group's own flags get a table, such as g1t work's --workspace (also G1T_WORKSPACE).
- G1T-Session:
- ses_01m560bz6eeqc8v1hrrha03pap
2 files+50−110/2 viewed
| 14 | 14 | ||
| 15 | 15 | | Flag | | | |
| 16 | 16 | | --- | --- | | |
| 17 | − | | `-R, --repo <[HOST/]OWNER/NAME>` | The repository, as [HOST/]OWNER/NAME, instead of the current directory's. | | |
| 17 | + | | `-R, --repo <[HOST/]OWNER/NAME>` | The repository, as [HOST/]OWNER/NAME, instead of the current directory's. Also `G1T_REPO`. | | |
| 18 | 18 | | `--hostname <HOST>` | The g1t host, such as g1t.sh or git.example.com. | | |
| 19 | 19 | | `--json [FIELDS]` | Print JSON: every field, or only the comma-separated FIELDS. | | |
| 20 | 20 | | `--jq <EXPRESSION>` | Run a jq expression over the JSON and print what it gives. | | |
| ⋯ | |||
| 1087 | 1087 | ||
| 1088 | 1088 | Work items: list, view, open, edit, link, close; claims, hand-offs, dependencies, events. | |
| 1089 | 1089 | ||
| 1090 | + | Flags every `g1t work` command takes: | |
| 1091 | + | ||
| 1092 | + | | Flag | | | |
| 1093 | + | | --- | --- | | |
| 1094 | + | | `--workspace <SLUG>` | The workspace, by its slug; the current repository's when left out. Also `G1T_WORKSPACE`. | | |
| 1095 | + | ||
| 1090 | 1096 | ### g1t work list | |
| 1091 | 1097 | ||
| 1092 | 1098 | List the workspace's work items; with --repo, those linked to that repository. | |
| ⋯ | |||
| 1152 | 1158 | g1t work new --title "Rename the flag" --target acme/api --target acme/docs | |
| 1153 | 1159 | ``` | |
| 1154 | 1160 | ||
| 1155 | − | Calls [`open_work_item`](/reference/api/work-items/open-work-item/), [`update_work_item`](/reference/api/work-items/update-work-item/), [`upload_attachment`](/reference/api/work-items/upload-attachment/). | |
| 1161 | + | Calls [`open_work_item`](/reference/api/work-items/open-work-item/), [`update_work_item`](/reference/api/work-items/update-work-item/), [`upload_attachment`](/reference/api/work-items/upload-attachment/), [`upload_work_item_attachment`](/reference/api/work-items/upload-work-item-attachment/). | |
| 1156 | 1162 | ||
| 1157 | 1163 | ### g1t work comment | |
| 1158 | 1164 | ||
| ⋯ | |||
| 1169 | 1175 | | `-F, --body-file <FILE>` | Read the text from a file, or - for stdin. | | |
| 1170 | 1176 | | `--attach <FILE>` | An image to show after the text (PNG, JPEG, GIF or WebP, at most 10 MB): uploaded, and its Markdown added. Repeat for more. | | |
| 1171 | 1177 | ||
| 1172 | − | Calls [`add_work_item_comment`](/reference/api/work-items/add-work-item-comment/), [`upload_attachment`](/reference/api/work-items/upload-attachment/). | |
| 1178 | + | Calls [`add_work_item_comment`](/reference/api/work-items/add-work-item-comment/), [`upload_work_item_attachment`](/reference/api/work-items/upload-work-item-attachment/). | |
| 1173 | 1179 | ||
| 1174 | 1180 | ### g1t work edit | |
| 1175 | 1181 | ||
| ⋯ | |||
| 1454 | 1460 | g1t pr comment --body 'The preview, after the fix:' --attach preview.png | |
| 1455 | 1461 | ``` | |
| 1456 | 1462 | ||
| 1457 | − | Calls [`upload_attachment`](/reference/api/work-items/upload-attachment/). | |
| 1463 | + | Calls [`upload_attachment`](/reference/api/work-items/upload-attachment/), [`upload_work_item_attachment`](/reference/api/work-items/upload-work-item-attachment/). | |
| 1458 | 1464 | ||
| 1459 | 1465 | ## g1t api | |
| 1460 | 1466 | ||
| 114 | 114 | if arg.is_last_set() { | |
| 115 | 115 | text = format!("After `--`. {text}"); | |
| 116 | 116 | } | |
| 117 | + | if let Some(env) = arg.get_env() { | |
| 118 | + | let _ = write!(text, " Also `{}`.", env.to_string_lossy()); | |
| 119 | + | } | |
| 117 | 120 | cell(text.trim()) | |
| 118 | 121 | } | |
| 119 | 122 | ||
| 120 | − | fn command(out: &mut String, cmd: &Command, path: &str, depth: usize) { | |
| 123 | + | /// The flags a command group takes for every command under it (`g1t work | |
| 124 | + | /// --workspace`): its global ones that no command above it has. | |
| 125 | + | fn group_flags<'a>(cmd: &'a Command, above: &[String]) -> Vec<&'a Arg> { | |
| 126 | + | cmd.get_arguments() | |
| 127 | + | .filter(|a| { | |
| 128 | + | a.is_global_set() | |
| 129 | + | && !a.is_hide_set() | |
| 130 | + | && !above.iter().any(|id| id == a.get_id().as_str()) | |
| 131 | + | }) | |
| 132 | + | .collect() | |
| 133 | + | } | |
| 134 | + | ||
| 135 | + | fn command(out: &mut String, cmd: &Command, path: &str, depth: usize, above: &[String]) { | |
| 121 | 136 | if cmd.is_hide_set() || cmd.get_name() == "help" { | |
| 122 | 137 | return; | |
| 123 | 138 | } | |
| ⋯ | |||
| 131 | 146 | let _ = writeln!(out, "{}\n", sentence(&about.to_string())); | |
| 132 | 147 | } | |
| 133 | 148 | if !subs.is_empty() { | |
| 149 | + | let own = group_flags(cmd, above); | |
| 150 | + | if !own.is_empty() { | |
| 151 | + | let _ = writeln!( | |
| 152 | + | out, | |
| 153 | + | "Flags every `{path}` command takes:\n\n| Flag | |\n| --- | --- |" | |
| 154 | + | ); | |
| 155 | + | for arg in &own { | |
| 156 | + | let _ = writeln!(out, "| `{}` | {} |", cell(&flag(arg)), describe(arg)); | |
| 157 | + | } | |
| 158 | + | let _ = writeln!(out); | |
| 159 | + | } | |
| 160 | + | let mut above = above.to_vec(); | |
| 161 | + | above.extend(own.iter().map(|a| a.get_id().to_string())); | |
| 134 | 162 | for sub in &subs { | |
| 135 | − | command(out, sub, &format!("{path} {}", sub.get_name()), depth + 1); | |
| 163 | + | command( | |
| 164 | + | out, | |
| 165 | + | sub, | |
| 166 | + | &format!("{path} {}", sub.get_name()), | |
| 167 | + | depth + 1, | |
| 168 | + | &above, | |
| 169 | + | ); | |
| 136 | 170 | } | |
| 137 | 171 | return; | |
| 138 | 172 | } | |
| ⋯ | |||
| 179 | 213 | let mut root = <crate::cli::Cli as clap::CommandFactory>::command(); | |
| 180 | 214 | root.build(); | |
| 181 | 215 | let mut out = HEAD.to_owned(); | |
| 182 | − | for arg in root | |
| 183 | − | .get_arguments() | |
| 184 | − | .filter(|a| a.is_global_set() && !a.is_hide_set()) | |
| 185 | − | { | |
| 216 | + | let globals = group_flags(&root, &[]); | |
| 217 | + | for arg in &globals { | |
| 186 | 218 | let _ = writeln!(out, "| `{}` | {} |", cell(&flag(arg)), describe(arg)); | |
| 187 | 219 | } | |
| 188 | 220 | let _ = writeln!(out); | |
| 221 | + | let above: Vec<String> = globals.iter().map(|a| a.get_id().to_string()).collect(); | |
| 189 | 222 | for sub in root.get_subcommands() { | |
| 190 | − | command(&mut out, sub, &format!("g1t {}", sub.get_name()), 2); | |
| 223 | + | command(&mut out, sub, &format!("g1t {}", sub.get_name()), 2, &above); | |
| 191 | 224 | } | |
| 192 | 225 | out.trim_end().to_owned() + "\n" | |
| 193 | 226 | } | |
Comments
Sign in to comment.