datafusion_tui/cli/
args.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use clap::Parser;
19use std::path::Path;
20
21use super::print_format::PrintFormat;
22
23#[derive(Debug, Parser, PartialEq)]
24#[clap(author, version, about, long_about= None)]
25pub struct Args {
26    #[clap(
27        short = 'p',
28        long,
29        help = "Path to your data, default to current directory",
30        validator(is_valid_data_dir)
31    )]
32    pub data_path: Option<String>,
33
34    #[clap(
35        short = 'c',
36        long,
37        help = "The batch size of each query, or use DataFusion default",
38        validator(is_valid_batch_size)
39    )]
40    pub batch_size: Option<usize>,
41
42    #[clap(
43        short,
44        long,
45        multiple_values = true,
46        help = "Execute commands from file(s), then exit",
47        validator(is_valid_file)
48    )]
49    pub file: Vec<String>,
50
51    #[clap(
52        short = 'r',
53        long,
54        multiple_values = true,
55        help = "Run the provided files on startup instead of ~/.datafusionrc",
56        validator(is_valid_file),
57        conflicts_with = "file"
58    )]
59    pub rc: Option<Vec<String>>,
60
61    #[clap(long, arg_enum, default_value_t = PrintFormat::Table)]
62    pub format: PrintFormat,
63
64    #[clap(long, help = "Ballista scheduler host")]
65    pub host: Option<String>,
66
67    #[clap(long, help = "Ballista scheduler port")]
68    pub port: Option<u16>,
69
70    #[clap(
71        short,
72        long,
73        help = "Reduce printing other than the results and work quietly"
74    )]
75    pub quiet: bool,
76}
77
78fn is_valid_file(dir: &str) -> std::result::Result<(), String> {
79    if Path::new(dir).is_file() {
80        Ok(())
81    } else {
82        Err(format!("Invalid file '{}'", dir))
83    }
84}
85
86fn is_valid_data_dir(dir: &str) -> std::result::Result<(), String> {
87    if Path::new(dir).is_dir() {
88        Ok(())
89    } else {
90        Err(format!("Invalid data directory '{}'", dir))
91    }
92}
93
94fn is_valid_batch_size(size: &str) -> std::result::Result<(), String> {
95    match size.parse::<usize>() {
96        Ok(size) if size > 0 => Ok(()),
97        _ => Err(format!("Invalid batch size '{}'", size)),
98    }
99}
100
101pub fn mock_standard_args() -> Args {
102    Args {
103        data_path: None,
104        batch_size: None,
105        file: Vec::new(),
106        rc: None,
107        format: PrintFormat::Table,
108        host: None,
109        port: None,
110        quiet: false,
111    }
112}