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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*!
output first or last n lines, like a head and tail of linux command.

# Features

- output first or last n lines, like a head and tail of linux command.
- minimum support rustc 1.56.1 (59eed8a2a 2021-11-01)

# Command help

```text
aki-unbody --help
```

```text
Usage:
  aki-unbody [options]

output first or last n lines, like a head and tail of linux command.

Options:
  -h, --head <num>      output the first <num> lines.
  -t, --tail <num>      output the last <num> lines.
  -i, --inverse         output the body, except for head and tail.

  -H, --help        display this help and exit
  -V, --version     display version information and exit

Examples:
  Outputs first 2 lines:
    cat file1.txt | aki-unbody --head 2
  Outputs last 2 lines:
    cat file1.txt | aki-unbody --tail 2
  Outputs body, except for first 2 lines and last 2 lines:
    cat file1.txt | aki-unbody --head 2 --tail 2 --inverse
```

# Quick install

1. you can install this into cargo bin path:

```text
cargo install aki-unbody
```

2. you can build debian package:

```text
cargo deb
```

and install **.deb** into your local repository of debian package.

# Examples

The input data used in this example looks like this:

```text
cat file1.txt
```

result output:
```text
LN:0001,text
LN:0002,text
LN:0003,text
LN:0004,text
LN:0005,text
LN:0006,text
```

## Example 1: output head

Outputs first 2 lines.

command line:
```text
cat file1.txt | aki-unbody --head 2
```

result output:
```text
LN:0001,text
LN:0002,text
```

## Example 2: output tail

Outputs last 2 lines.

command line:
```text
cat file1.txt | aki-unbody --tail 2
```

result output:
```text
LN:0005,text
LN:0006,text
```

## Example 3: output head and tail

Outputs first 2 lines and last 2 lines.

command line:
```text
cat file1.txt | aki-unbody --head 2 --tail 2
```

result output:
```text
LN:0001,text
LN:0002,text
LN:0005,text
LN:0006,text
```

## Example 4: output body, except for head and tail

Outputs body, except for first 2 lines and last 2 lines.

command line:
```text
cat file1.txt | aki-unbody --head 2 --tail 2 --inverse
```

result output:
```text
LN:0003,text
LN:0004,text
```

# Library example

See [`fn execute()`] for this library examples.

[`fn execute()`]: crate::execute
*/
#[macro_use]
extern crate anyhow;

mod conf;
mod run;
mod util;

use flood_tide::HelpVersion;
use runnel::RunnelIoe;
use std::io::Write;

const TRY_HELP_MSG: &str = "Try --help for help.";

/// execute unbody
///
/// params:
///   - sioe: stream in/out/err
///   - program: program name. etc. "unbody"
///   - args: parameter arguments.
///
/// return:
///   - ok: ()
///   - err: anyhow
///
/// # Examples
///
/// ## Example 1: output head
///
/// Outputs first 2 lines.
///
/// ```rust
/// use runnel::RunnelIoeBuilder;
///
/// let r = libaki_unbody::execute(&RunnelIoeBuilder::new().build(),
///     "unbody", &["--head", "2"]);
/// ```
///
/// ## Example 2: output tail
///
/// Outputs last 2 lines.
///
/// ```rust
/// use runnel::RunnelIoeBuilder;
///
/// let r = libaki_unbody::execute(&RunnelIoeBuilder::new().build(),
///     "unbody", &["--tail", "2"]);
/// ```
///
/// ## Example 3: output head and tail
///
/// Outputs first 2 lines and last 2 lines.
///
/// ```rust
/// use runnel::RunnelIoeBuilder;
///
/// let r = libaki_unbody::execute(&RunnelIoeBuilder::new().build(),
///     "unbody", &["--head", "2", "--tail", "2"]);
/// ```
///
/// ## Example 4: output body, except for head and tail
///
/// Outputs body, except for first 2 lines and last 2 lines.
///
/// ```rust
/// use runnel::RunnelIoeBuilder;
///
/// let r = libaki_unbody::execute(&RunnelIoeBuilder::new().build(),
///     "unbody", &["--head", "2", "--tail", "2", "--inverse"]);
/// ```
///
pub fn execute(sioe: &RunnelIoe, prog_name: &str, args: &[&str]) -> anyhow::Result<()> {
    let conf = match conf::parse_cmdopts(prog_name, args) {
        Ok(conf) => conf,
        Err(errs) => {
            for err in errs.iter().take(1) {
                if err.is_help() || err.is_version() {
                    let _r = sioe.pout().lock().write_fmt(format_args!("{err}\n"));
                    return Ok(());
                }
            }
            return Err(anyhow!("{}\n{}", errs, TRY_HELP_MSG));
        }
    };
    run::run(sioe, &conf)
}