rsv_lib/
args.rs

1use crate::utils::{row_split::CsvRowSplitter, util::get_valid_sep};
2use clap::Args;
3
4#[derive(Debug, Args)]
5pub struct Count {
6    /// File to open
7    pub filename: Option<String>,
8    /// Whether the file has a header
9    #[arg(long, default_value_t = false)]
10    pub no_header: bool,
11    /// Get the nth worksheet of Excel file
12    #[arg(short = 'S', long, default_value_t = 0)]
13    pub sheet: usize,
14}
15
16#[derive(Debug, Args)]
17pub struct Size {
18    /// File to open
19    pub filename: Option<String>,
20}
21
22#[derive(Debug, Args)]
23pub struct Estimate {
24    /// File to open
25    pub filename: Option<String>,
26    /// Get the nth worksheet for an Excel file
27    #[arg(short = 'S', long, default_value_t = 0)]
28    pub sheet: usize,
29}
30
31#[derive(Debug, Args)]
32pub struct Head {
33    /// File to open
34    pub filename: Option<String>,
35    /// Whether the file has a header
36    #[arg(long, default_value_t = false)]
37    pub no_header: bool,
38    /// Number of records to show
39    #[arg(short, long, default_value_t = 10)]
40    pub n: usize,
41    /// Get the nth worksheet of EXCEL file
42    #[arg(short = 'S', long, default_value_t = 0)]
43    pub sheet: usize,
44    /// Export to a file named current-file-head.csv?
45    #[arg(short = 'E', long, default_value_t = false)]
46    pub export: bool,
47    /// Field separator
48    #[arg(short, long, default_value_t = ',', value_parser=get_valid_sep)]
49    pub sep: char,
50    /// Quote char
51    #[arg(short, long, default_value_t = '"')]
52    pub quote: char,
53}
54
55#[derive(Debug, Args)]
56pub struct Tail {
57    /// File to open
58    pub filename: Option<String>,
59    /// Whether the file has a header
60    #[arg(long, default_value_t = false)]
61    pub no_header: bool,
62    /// Number of records to show
63    #[arg(short, long, default_value_t = 10)]
64    pub n: usize,
65    /// Get the nth worksheet of EXCEL file
66    #[arg(short = 'S', long, default_value_t = 0)]
67    pub sheet: usize,
68    /// Export to a file named current-file-head.csv?
69    #[arg(short = 'E', long, default_value_t = false)]
70    pub export: bool,
71}
72
73#[derive(Debug, Args)]
74pub struct Headers {
75    /// File to open
76    pub filename: Option<String>,
77    /// Field separator
78    #[arg(short, long, default_value_t = ',', value_parser=get_valid_sep)]
79    pub sep: char,
80    /// Quote char
81    #[arg(short, long, default_value_t = '"')]
82    pub quote: char,
83    /// Get the nth worksheet of EXCEL file
84    #[arg(short = 'S', long, default_value_t = 0)]
85    pub sheet: usize,
86}
87
88#[derive(Debug, Args)]
89pub struct Clean {
90    /// File to open
91    pub filename: Option<String>,
92    /// Output file, default to current-file-cleaned.csv
93    #[arg(short, long, default_value_t = String::from(""), hide_default_value=true)]
94    pub output: String,
95    /// Escape char to clean
96    #[arg(short, long, default_value_t = String::from("\""))]
97    pub escape: String,
98}
99
100#[derive(Debug, Args)]
101pub struct Flatten {
102    /// File to open
103    pub filename: Option<String>,
104    /// Separator
105    #[arg(short, long, default_value_t = ',', value_parser=get_valid_sep)]
106    pub sep: char,
107    /// Quote char
108    #[arg(short, long, default_value_t = '"')]
109    pub quote: char,
110    /// Whether the file has a header
111    #[arg(long, default_value_t = false)]
112    pub no_header: bool,
113    /// Line delimiter for printing
114    #[arg(short, long, default_value_t = String::from("#"))]
115    pub delimiter: String,
116    /// Number of records to show, n=-1 to show all
117    #[arg(short, long, default_value_t = 5)]
118    pub n: i32,
119    /// Get the nth worksheet of EXCEL file
120    #[arg(short = 'S', long, default_value_t = 0)]
121    pub sheet: usize,
122}
123
124#[derive(Debug, Args)]
125pub struct Frequency {
126    /// File to open
127    pub filename: Option<String>,
128    /// Separator
129    #[arg(short, long, default_value_t = ',', value_parser=get_valid_sep)]
130    pub sep: char,
131    /// Quote char
132    #[arg(short, long, default_value_t = '"')]
133    pub quote: char,
134    /// Whether the file has a header
135    #[arg(long, default_value_t = false)]
136    pub no_header: bool,
137    /// Columns to generate frequency table
138    #[arg(short, long, default_value_t = String::from("0"), allow_hyphen_values=true)]
139    pub cols: String,
140    /// Ascending order or not
141    #[arg(short, long, default_value_t = false)]
142    pub ascending: bool,
143    /// Export result to a frequency.csv?
144    #[arg(short = 'E', long, default_value_t = false)]
145    pub export: bool,
146    /// Top N to keep in frequency table
147    #[arg(short, long, default_value_t = -1)]
148    pub n: i32,
149    /// Get the nth worksheet of EXCEL file
150    #[arg(short = 'S', long, default_value_t = 0)]
151    pub sheet: usize,
152}
153
154#[derive(Debug, Args)]
155pub struct Split {
156    /// File to open
157    pub filename: Option<String>,
158    /// Separator
159    #[arg(short, long, default_value_t = ',', value_parser=get_valid_sep)]
160    pub sep: char,
161    /// Quote char
162    #[arg(short, long, default_value_t = '"')]
163    pub quote: char,
164    /// Whether the file has a header
165    #[arg(long, default_value_t = false)]
166    pub no_header: bool,
167    /// Column to split upon
168    #[arg(short, long, default_value_t = 0)]
169    pub col: usize,
170    /// Get the nth worksheet of EXCEL file
171    #[arg(short = 'S', long, default_value_t = 0)]
172    pub sheet: usize,
173    /// Number of records to write in each separate file
174    #[arg(long)]
175    pub size: Option<usize>,
176}
177
178#[derive(Debug, Args)]
179pub struct Slice {
180    /// File to open
181    pub filename: Option<String>,
182    /// Start index of CSV
183    #[arg(short, long, default_value_t = 0)]
184    pub start: usize,
185    /// End index of CSV
186    #[arg(short, long)]
187    pub end: Option<usize>,
188    /// Slice length
189    #[arg(short, long)]
190    pub length: Option<usize>,
191    /// Index for single record of CSV
192    #[arg(short, long)]
193    pub index: Option<usize>,
194    /// Whether the file has a header
195    #[arg(long, default_value_t = false)]
196    pub no_header: bool,
197    /// Export data to a current-file-slice.csv?
198    #[arg(short = 'E', long, default_value_t = false)]
199    pub export: bool,
200    /// Get the nth worksheet of EXCEL file
201    #[arg(short = 'S', long, default_value_t = 0)]
202    pub sheet: usize,
203}
204
205#[derive(Debug, Args)]
206pub struct Select {
207    /// File to open
208    pub filename: Option<String>,
209    /// Separator
210    #[arg(short, long, default_value_t = ',', value_parser=get_valid_sep)]
211    pub sep: char,
212    /// Separator
213    #[arg(short, long, default_value_t = ',')]
214    pub quote: char,
215    /// Whether the file has a header
216    #[arg(long, default_value_t = false)]
217    pub no_header: bool,
218    /// Columns to select, support syntax 0,1,3 or 0-4, including 4; Default to select all columns
219    #[arg(short, long, default_value_t = String::from(""), allow_hyphen_values=true)]
220    pub cols: String,
221    /// Row filter, support syntax 0=a,b,c or 0=a,b&1=c,d; Default to None
222    #[arg(short, long, default_value_t = String::from(""), allow_hyphen_values=true)]
223    pub filter: String,
224    /// Export results to a file named current-file-selected.csv?
225    #[arg(short = 'E', long, default_value_t = false)]
226    pub export: bool,
227    /// Get the nth worksheet of EXCEL file
228    #[arg(short = 'S', long, default_value_t = 0)]
229    pub sheet: usize,
230}
231
232#[derive(Debug, Args)]
233pub struct Stats {
234    /// File to open
235    pub filename: Option<String>,
236    /// Separator
237    #[arg(short, long, default_value_t = ',', value_parser=get_valid_sep)]
238    pub sep: char,
239    /// Quote Char
240    #[arg(short, long, default_value_t = ',')]
241    pub quote: char,
242    /// Whether the file has a header
243    #[arg(long, default_value_t = false)]
244    pub no_header: bool,
245    /// Columns to generate statistics, support syntax 0,1,3 or 0-4, including 4; Default to select all columns
246    #[arg(short, long, default_value_t = String::from(""), allow_hyphen_values=true)]
247    pub cols: String,
248    /// Export results to a file named current-file-selected.csv?
249    #[arg(short = 'E', long, default_value_t = false)]
250    pub export: bool,
251    /// Get the nth worksheet of EXCEL file
252    #[arg(short = 'S', long, default_value_t = 0)]
253    pub sheet: usize,
254}
255
256#[derive(Debug, Args)]
257pub struct Excel2csv {
258    /// File to open
259    pub filename: Option<String>,
260    /// Get the nth worksheet of EXCEL file
261    #[arg(short = 'S', long, default_value_t = 0)]
262    pub sheet: usize,
263    /// Separator Char
264    #[arg(short, long, default_value_t = ',', value_parser=get_valid_sep)]
265    pub sep: char,
266    /// Quote Char
267    #[arg(short, long, default_value_t = '"')]
268    pub quote: char,
269}
270
271#[derive(Debug, Args)]
272pub struct Table {
273    /// File to open
274    pub filename: Option<String>,
275    /// Get the nth worksheet of EXCEL file
276    #[arg(short = 'S', long, default_value_t = 0)]
277    pub sheet: usize,
278    /// Separator Char
279    #[arg(short, long, default_value_t = ',', value_parser=get_valid_sep)]
280    pub sep: char,
281    /// Quote Char
282    #[arg(short, long, default_value_t = '"')]
283    pub quote: char,
284}
285
286#[derive(Debug, Args)]
287pub struct Search {
288    /// Regex pattern to search
289    pub pattern: String,
290    /// File to open
291    pub filename: Option<String>,
292    /// Whether the file has a header
293    #[arg(long, default_value_t = false)]
294    pub no_header: bool,
295    /// Separator Char
296    #[arg(short, long, default_value_t = ',', value_parser=get_valid_sep)]
297    pub sep: char,
298    /// Quote Char
299    #[arg(short, long, default_value_t = '"')]
300    pub quote: char,
301    /// Search specific columns, e.g. -f=0,1 to search first two columns; Default to all columns
302    #[arg(short, long, default_value_t = String::from(""), allow_hyphen_values=true)]
303    pub filter: String,
304    /// Columns to select in output, support syntax 0,1,3 or 0-4, including 4; Default to select all columns
305    #[arg(short, long, default_value_t = String::from(""), allow_hyphen_values=true)]
306    pub cols: String,
307    /// Get the nth worksheet of EXCEL file
308    #[arg(short = 'S', long, default_value_t = String::from("0"), allow_hyphen_values = true)]
309    pub sheet: String,
310    /// Export to a file named current-file-searched.csv?
311    #[arg(short = 'E', long, default_value_t = false)]
312    pub export: bool,
313}
314
315#[derive(Debug, Args)]
316pub struct Sort {
317    /// File to open
318    pub filename: Option<String>,
319    /// Separator
320    #[arg(short, long, default_value_t=',', value_parser=get_valid_sep )]
321    pub sep: char,
322    /// Quote Char
323    #[arg(short, long, default_value_t = '"')]
324    pub quote: char,
325    /// Whether the file has a header
326    #[arg(long, default_value_t = false)]
327    pub no_header: bool,
328    /// Columns to sort by, support syntax 0 (first column),
329    /// "-0" (descending), "-0N" (as numeric) or "0N,-1" (two columns)
330    #[arg(short, long, default_value_t = String::from("0"), allow_hyphen_values=true)]
331    pub cols: String,
332    /// Get the nth worksheet of EXCEL file
333    #[arg(short = 'S', long, default_value_t = 0)]
334    pub sheet: usize,
335    /// Export to a file named current-file-searched.csv?
336    #[arg(short = 'E', long, default_value_t = false)]
337    pub export: bool,
338}
339
340#[derive(Debug, Args)]
341pub struct Sample {
342    /// File to open
343    pub filename: Option<String>,
344    /// Whether the file has a header
345    #[arg(long, default_value_t = false)]
346    pub no_header: bool,
347    /// Get the nth worksheet of EXCEL file
348    #[arg(short = 'S', long, default_value_t = 0)]
349    pub sheet: usize,
350    /// Sample size
351    #[arg(short, long, default_value_t = 10)]
352    pub n: usize,
353    /// Get the nth worksheet of EXCEL file
354    #[arg(long)]
355    pub seed: Option<usize>,
356    /// Export to a file named current-file-searched.csv?
357    #[arg(short = 'E', long, default_value_t = false)]
358    pub export: bool,
359    /// Show line number
360    #[arg(long, long, default_value_t = false)]
361    pub show_number: bool,
362    /// Time limit
363    #[arg(short, long, default_value_t = 0.0)]
364    pub time_limit: f32,
365}
366
367#[derive(Debug, Args)]
368pub struct To {
369    /// Output file, a file name or a file format
370    pub out: String,
371    /// File to open
372    pub filename: Option<String>,
373    /// Whether the file has a header
374    #[arg(long, default_value_t = false)]
375    pub no_header: bool,
376    /// Input file Separator
377    #[arg(short, long, default_value_t = ',', value_parser=get_valid_sep)]
378    pub sep: char,
379    /// Quote char
380    #[arg(short, long, default_value_t = '"')]
381    pub quote: char,
382    /// Get the nth worksheet of EXCEL file
383    #[arg(short = 'S', long, default_value_t = 0)]
384    pub sheet: usize,
385}
386
387#[derive(Debug, Args)]
388pub struct Unique {
389    /// File to open
390    pub filename: Option<String>,
391    /// Separator
392    #[arg(short, long, default_value_t = ',', value_parser=get_valid_sep)]
393    pub sep: char,
394    /// Separator
395    #[arg(short, long, default_value_t = '"')]
396    pub quote: char,
397    /// Whether the file has a header
398    #[arg(long, default_value_t = false)]
399    pub no_header: bool,
400    /// Columns to filter
401    #[arg(short, long, default_value_t = String::from("-1"), allow_hyphen_values=true)]
402    pub cols: String,
403    /// keep first or last
404    #[arg(long, default_value_t = false)]
405    pub keep_last: bool,
406    /// Get the nth worksheet of EXCEL file
407    #[arg(short = 'S', long, default_value_t = 0)]
408    pub sheet: usize,
409    /// Export to a file named drop-duplicates.csv?
410    #[arg(short = 'E', long, default_value_t = false)]
411    pub export: bool,
412}
413
414macro_rules! impl_row_split {
415    ($cmd:ident) => {
416        impl $cmd {
417            #[allow(dead_code)]
418            pub fn split<'a>(&self, row: &'a str) -> CsvRowSplitter<'a> {
419                CsvRowSplitter::new(row, self.sep, self.quote)
420            }
421
422            #[allow(dead_code)]
423            pub fn split_row_to_vec<'a>(&self, row: &'a str) -> Vec<&'a str> {
424                CsvRowSplitter::new(row, self.sep, self.quote).collect()
425            }
426
427            #[allow(dead_code)]
428            pub fn split_row_to_owned_vec<'a>(&self, row: &'a str) -> Vec<String> {
429                CsvRowSplitter::new(row, self.sep, self.quote)
430                    .map(|i| i.to_owned())
431                    .collect::<Vec<_>>()
432            }
433
434            #[allow(dead_code)]
435            pub fn row_field_count<'a>(&self, row: &'a str) -> usize {
436                CsvRowSplitter::new(row, self.sep, self.quote).count()
437            }
438        }
439    };
440}
441
442impl_row_split!(Head);
443impl_row_split!(Headers);
444impl_row_split!(Frequency);
445impl_row_split!(Select);
446impl_row_split!(Stats);
447impl_row_split!(To);
448impl_row_split!(Flatten);
449impl_row_split!(Unique);
450impl_row_split!(Search);
451impl_row_split!(Table);
452impl_row_split!(Split);
453impl_row_split!(Excel2csv);