├── .clog.toml ├── .github └── CONTRIBUTING.md ├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── appveyor.yml ├── index.html ├── rustfmt.toml └── src ├── fs └── mod.rs ├── lib.rs └── num └── mod.rs /.clog.toml: -------------------------------------------------------------------------------- 1 | [clog] 2 | repository = "https://github.com/kbknapp/clap-validators" 3 | outfile = "CHANGELOG.md" 4 | from-latest-tag = true 5 | 6 | [sections] 7 | Performance = ["perf"] 8 | Improvements = ["impr", "im", "imp"] 9 | Documentation = ["docs"] 10 | Deprecations = ["depr"] 11 | Examples = ["examples"] 12 | "New Validator" = ["val"] 13 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to Contribute 2 | 3 | Contributions are always welcome! Please use the following guidelines when contributing to `clap-validators` 4 | 5 | 1. Fork `clap-validators` 6 | 2. Clone your fork (`git clone https://github.com/$YOUR_USERNAME/clap-validators && cd clap-validators`) 7 | 3. Create new branch (`git checkout -b new-branch`) 8 | 4. Make your changes, and commit (`git commit -am "your message"`) 9 | * I use a [conventional](https://github.com/ajoslin/conventional-changelog/blob/a5505865ff3dd710cf757f50530e73ef0ca641da/conventions/angular.md) changelog format so I can update my changelog using [clog](https://github.com/clog-tool/clog-cli) 10 | * In addition to the conventions defined above, I also use `imp`, `wip`, `examples`. 11 | * Format your commit subject line using the following format: `TYPE(COMPONENT): MESSAGE` where `TYPE` is one of the following: 12 | - `feat` - A new feature 13 | - `imp` - An improvement to an existing feature 14 | - `perf` - A performance improvement 15 | - `docs` - Changes to documentation only 16 | - `tests` - Changes to the testing framework or tests only 17 | - `fix` - A bug fix 18 | - `refactor` - Code functionality doesn't change, but underlying structure may 19 | - `style` - Stylistic changes only, no functionality changes 20 | - `wip` - A work in progress commit (Should typically be `git rebase`'ed away) 21 | - `chore` - Catch all or things that have to do with the build system, etc 22 | - `examples` - Changes to existing example, or a new example 23 | * The `COMPONENT` is optional, and may be a single file, directory, or logical component. Can be omitted if commit applies globally 24 | 5. Run the tests (`cargo test --features yaml && make -C clap-tests test`) 25 | 6. `git rebase` into concise commits and remove `--fixup`s (`git rebase -i HEAD~NUM` where `NUM` is number of commits back) 26 | 7. Push your changes back to your fork (`git push origin $your-branch`) 27 | 8. Create a pull request! (You can also create the pull request first, and we'll merge when ready. This a good way to discuss proposed changes.) 28 | 29 | Another really great way to help is if you find an interesting, or helpful way in which to use `clap`. You can either add it to the [examples/](examples) directory, or file an issue and tell me. I'm all about giving credit where credit is due :) 30 | 31 | ## Goals 32 | 33 | There are a few goals of `clap-validators` that I'd like to maintain throughout contributions. 34 | 35 | * Remain backwards compatible when possible 36 | - If backwards compatibility *must* be broken, use deprecation warnings if at all possible before removing legacy code 37 | - This does not apply for security concerns 38 | * Validate values quickly 39 | - Validating of arguments values shouldn't slow down usage of the main program 40 | * Try to be cognizant of memory usage 41 | * `panic!` on *developer* error, exit gracefully on *end-user* error 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled files 2 | *.o 3 | *.so 4 | *.rlib 5 | *.dll 6 | 7 | # Executables 8 | *.exe 9 | 10 | # Generated by Cargo 11 | /target/ 12 | 13 | # Cargo files 14 | Cargo.lock 15 | 16 | # Temp files 17 | .*~ 18 | 19 | # Backup files 20 | *.bak 21 | *.bk 22 | *.orig 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: rust 3 | rust: 4 | - nightly 5 | - beta 6 | - stable 7 | # Only while clippy is failing 8 | matrix: 9 | allow_failures: 10 | - rust: nightly 11 | before_script: 12 | - | 13 | pip install 'travis-cargo<0.2' --user && 14 | export PATH=$HOME/.local/bin:$PATH 15 | script: 16 | - | 17 | cargo build --verbose && 18 | travis-cargo --only nightly build -- --features travis && 19 | travis-cargo --only stable doc 20 | addons: 21 | apt: 22 | packages: 23 | - libcurl4-openssl-dev 24 | - libelf-dev 25 | - libdw-dev 26 | after_success: 27 | - | 28 | travis-cargo --only stable doc-upload && 29 | travis-cargo --only stable coveralls --no-sudo 30 | env: 31 | global: 32 | - TRAVIS_CARGO_NIGHTLY_FEATURE=travis 33 | - secure: JLBlgHY6OEmhJ8woewNJHmuBokTNUv7/WvLkJGV8xk0t6bXBwSU0jNloXwlH7FiQTc4TccX0PumPDD4MrMgxIAVFPmmmlQOCmdpYP4tqZJ8xo189E5zk8lKF5OyaVYCs5SMmFC3cxCsKjfwGIexNu3ck5Uhwe9jI0tqgkgM3URA= 34 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | 3 | name = "clap-validators" 4 | version = "0.0.1" 5 | authors = ["Kevin K. "] 6 | exclude = ["examples/*"] 7 | description = "A collection of pre-defined argument value validators that can be dropped in to clap parsing" 8 | repository = "https://github.com/kbknapp/clap-validators.git" 9 | documentation = "http://kbknapp.github.io/clap-validators" 10 | readme = "README.md" 11 | license = "MIT/Apache 2.0" 12 | keywords = ["argument", "validate", "arg", "parser", "parse"] 13 | 14 | [dependencies] 15 | clippy = { version = "~0.0.48", optional = true } 16 | 17 | [features] 18 | default = [] 19 | lints = ["clippy", "nightly"] 20 | nightly = [] # for building with nightly and unstable features 21 | unstable = [] # for building with unstable features on stable Rust 22 | debug = [] # for building with debug messages 23 | travis = ["lints", "nightly"] # for building with travis-cargo 24 | 25 | [profile.release] 26 | opt-level = 3 27 | debug = false 28 | rpath = false 29 | lto = true 30 | debug-assertions = false 31 | # codegen-units ignored with lto=true 32 | 33 | [profile.dev] 34 | opt-level = 0 35 | debug = true 36 | rpath = false 37 | lto = false 38 | debug-assertions = true 39 | codegen-units = 4 40 | 41 | [profile.test] 42 | opt-level = 1 43 | debug = true 44 | rpath = false 45 | lto = false 46 | debug-assertions = true 47 | codegen-units = 2 48 | 49 | [profile.bench] 50 | opt-level = 3 51 | debug = false 52 | rpath = false 53 | lto = true 54 | debug-assertions = false 55 | 56 | [profile.doc] 57 | opt-level = 0 58 | debug = true 59 | rpath = false 60 | lto = false 61 | debug-assertions = true 62 | codegen-units = 4 63 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Kevin K. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # clap-validators 2 | 3 | [![Crates.io](https://img.shields.io/crates/v/clap-validators.svg)](https://crates.io/crates/clap-validators) [![Crates.io](https://img.shields.io/crates/d/clap-validators.svg)](https://crates.io/crates/clap-validators) [![license](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/kbknapp/clap-validators/blob/master/LICENSE-MIT) [![Coverage Status](https://coveralls.io/repos/kbknapp/clap-validators/badge.svg?branch=master&service=github)](https://coveralls.io/github/kbknapp/clap-validators?branch=master) [![Join the chat at https://gitter.im/kbknapp/clap-validators](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/kbknapp/clap-validators?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | Linux: [![Build Status](https://travis-ci.org/kbknapp/clap-validators.svg?branch=master)](https://travis-ci.org/kbknapp/clap-validators) 6 | Windows: [![Build status](https://ci.appveyor.com/api/projects/status/ejg8c33dn31nhv36/branch/master?svg=true)](https://ci.appveyor.com/project/kbknapp/clap-validators/branch/master) 7 | 8 | Command Line Argument Parser for Rust 9 | 10 | It is a simple to use, efficient, and full featured library for parsing command line arguments and subcommands when writing console, or terminal applications. 11 | 12 | ## [documentation](http://kbknapp.github.io/clap-validators/docs/clap_validators/index.html) 13 | 14 | Table of Contents 15 | ================= 16 | 17 | * [What's New](#whats-new) 18 | * [About](#about) 19 | * [Validators](#validators) 20 | * [Quick Example](#quick-example) 21 | * [Try it!](#try-it) 22 | * [BYOB (Build Your Own Binary)](#byob-build-your-own-binary) 23 | * [Usage](#usage) 24 | * [More Information](#more-information) 25 | * [How to Contribute](#how-to-contribute) 26 | * [Running the tests](#running-the-tests) 27 | * [License](#license) 28 | * [Deprecations](#deprecations) 29 | 30 | Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc) 31 | 32 | ## What's New 33 | 34 | In v0.0.1 35 | 36 | #### New Features 37 | 38 | * **Default Values**: Initial Release! 39 | 40 | For full details, see [CHANGELOG.md](https://github.com/kbknapp/clap-validators/blob/master/CHANGELOG.md) 41 | 42 | ## About 43 | 44 | `clap-validators` are pre-defined functions which can be dropped in to [clap](https://github.com/kbknapp/clap-rs) argument declarations to perform simple value validations. 45 | 46 | ## Validators 47 | 48 | Below are the validators which are included with `clap-validators`; full descriptions and usage can be found in the [documentation](http://kbknapp.github.io/clap-validators/docs/clap_validators/index.html) and [examples/](examples) directory 49 | 50 | * `clap_validators::numeric` 51 | * `is_number` - Valid `i64` or `f64` 52 | 53 | ## Quick Example 54 | 55 | The following examples show a quick example of using `clap-validators` in combination with `clap` to perform some basic validation of arguments. 56 | 57 | ```rust 58 | extern crate clap; 59 | extern crate clap_validators; 60 | 61 | use clap::{Arg, App}; 62 | use clap_validators; 63 | 64 | fn main() { 65 | let matches = App::new("My Super Program") 66 | .arg(Arg::with_name("infile") 67 | .short("i") 68 | .value_name("FILE") 69 | .help("Some file to read") 70 | .takes_value(true) 71 | .validator(clap_validators::fs::is_file)) 72 | .get_matches(); 73 | // program logic goes here, and we can make the assumption that "infile" is a valid file... 74 | } 75 | ``` 76 | 77 | If you were to compile any of the above programs and run them with the flag `-i ` where `` wasn't actually a valid file, one would see the graceful exit of: 78 | 79 | ```sh 80 | $ myprog -i not-a-file 81 | error: not-a-file isn't a valid file 82 | ``` 83 | 84 | ## Try it! 85 | 86 | To test out `clap-validators`: 87 | * Create a new cargo project `$ cargo new fake --bin && cd fake` 88 | * Add `clap-validators` and `clap` to your `Cargo.toml` 89 | * 90 | ```toml 91 | [dependencies] 92 | clap = "2" 93 | clap-validators = "0" 94 | ``` 95 | 96 | * Add the following to your `src/main.rs` 97 | 98 | ```rust 99 | extern crate clap; 100 | extern crate clap_validators; 101 | 102 | use clap::{App, Arg}; 103 | use clap_validators; 104 | 105 | fn main() { 106 | let m = App::new("fake") 107 | .arg(Arg::with_name("infile") 108 | .required(true) 109 | .validator(clap_validators::fs::is_file)) 110 | .get_matches(); 111 | 112 | println!("success!"); 113 | } 114 | ``` 115 | 116 | * Build your program `$ cargo build --release` 117 | * Create a file, since we'll be testing if something is a file or not `touch myfile` 118 | * Run with help or version `$ ./target/release/fake myfile` 119 | * Or to see this fail, run again with something that is not a file, such as a directory `mkdir mydir`, `./target/release/fake mydir` 120 | 121 | ## Usage 122 | 123 | For full usage, add `clap-validators` as a dependency in your `Cargo.toml` file to use from crates.io: 124 | 125 | ```toml 126 | [dependencies] 127 | clap_validators = "0" 128 | ``` 129 | 130 | Or track the latest on the master branch at github: 131 | 132 | ```toml 133 | [dependencies.clap_validators] 134 | git = "https://github.com/kbknapp/clap-validators.git" 135 | ``` 136 | 137 | Add `extern crate clap_validators;` to your crate root. 138 | 139 | ### More Information 140 | 141 | You can find complete documentation on the [github-pages site](http://kbknapp.github.io/clap-validators/clap_validators/index.html) for this project. 142 | 143 | You can also find usage examples in the [examples/](examples) directory of this repo. 144 | 145 | ## How to Contribute 146 | 147 | Contributions are always welcome! And there is a multitude of ways in which you can help depending on what you like to do, or are good at. Anything from adding validators, documentation, code cleanup, issue completion, new features, you name it, even filing issues is contributing and greatly appreciated! 148 | 149 | Another really great way to help is if you find an interesting, or helpful way in which to use `clap-validators`. You can either add it to the [examples/](examples) directory, or file an issue and tell me. I'm all about giving credit where credit is due :) 150 | 151 | Please read [CONTRIBUTING.md](.github/CONTRIBUTING.md) before you start contributing. 152 | 153 | ### Running the tests 154 | 155 | If contributing, you can run the tests as follows (assuming you're in the `clap-validators` directory) 156 | 157 | ``` 158 | $ cargo test 159 | 160 | # Only on nightly compiler: 161 | $ cargo build --features lints 162 | ``` 163 | 164 | ## License 165 | 166 | `clap-validators` is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses. 167 | 168 | See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details. 169 | 170 | ### Deprecations 171 | 172 | Old method names will be left around for several minor version bumps, or one major version bump. 173 | 174 | As of 0.0.1: 175 | 176 | * None! 177 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | install: 2 | - ps: Start-FileDownload 'https://static.rust-lang.org/dist/rust-nightly-i686-pc-windows-gnu.exe' 3 | - rust-nightly-i686-pc-windows-gnu.exe /VERYSILENT /NORESTART /DIR="C:\Program Files (x86)\Rust" 4 | - SET PATH=%PATH%;C:\Program Files (x86)\Rust\bin 5 | - rustc -V 6 | - cargo -V 7 | 8 | build: false 9 | 10 | test_script: 11 | - cargo build --verbose 12 | - cargo test 13 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | format_strings = false 2 | reorder_imports = true 3 | chain_overflow_last = false 4 | same_line_if_else = true 5 | -------------------------------------------------------------------------------- /src/fs/mod.rs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clap-rs/clap-validators/17d37fa4525a98866c34d9ba233709e7385ece18/src/fs/mod.rs -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | // Copyright ⓒ 2015-2016 Kevin B. Knapp and clap-validators contributors. 2 | // clap-validators is primarily distributed under the terms of both the MIT license and the Apache 3 | // License (Version 2.0), with portions covered by various BSD-like licenses. 4 | // See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details. 5 | 6 | //! # clap-validators 7 | //! 8 | //! [![Crates.io](https://img.shields.io/crates/v/clap-validators.svg)](https://crates.io/crates/clap-validators) [![Crates.io](https://img.shields.io/crates/d/clap-validators.svg)](https://crates.io/crates/clap-validators) [![license](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/kbknapp/clap-validators/blob/master/LICENSE-MIT) [![Coverage Status](https://coveralls.io/repos/kbknapp/clap-validators/badge.svg?branch=master&service=github)](https://coveralls.io/github/kbknapp/clap-validators?branch=master) [![Join the chat at https://gitter.im/kbknapp/clap-validators](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/kbknapp/clap-validators?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 9 | //! 10 | //! Linux: [![Build Status](https://travis-ci.org/kbknapp/clap-validators.svg?branch=master)](https://travis-ci.org/kbknapp/clap-validators) 11 | //! Windows: [![Build status](https://ci.appveyor.com/api/projects/status/ejg8c33dn31nhv36/branch/master?svg=true)](https://ci.appveyor.com/project/kbknapp/clap-validators/branch/master) 12 | //! 13 | //! Command Line Argument Parser for Rust 14 | //! 15 | //! It is a simple to use, efficient, and full featured library for parsing command line arguments and subcommands when writing console, or terminal applications. 16 | //! 17 | //! ## [documentation](http://kbknapp.github.io/clap-validators/clap-validators/index.html) 18 | //! 19 | //! Table of Contents 20 | //! ================= 21 | //! 22 | //! * [What's New](#whats-new) 23 | //! * [About](#about) 24 | //! * [Validators](#validators) 25 | //! * [Quick Example](#quick-example) 26 | //! * [Try it!](#try-it) 27 | //! * [BYOB (Build Your Own Binary)](#byob-build-your-own-binary) 28 | //! * [Usage](#usage) 29 | //! * [More Information](#more-information) 30 | //! * [How to Contribute](#how-to-contribute) 31 | //! * [Running the tests](#running-the-tests) 32 | //! * [License](#license) 33 | //! * [Deprecations](#deprecations) 34 | //! 35 | //! Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc) 36 | //! 37 | //! ## What's New 38 | //! 39 | //! In v0.0.1 40 | //! 41 | //! #### New Features 42 | //! 43 | //! * **Default Values**: Initial Release! 44 | //! 45 | //! For full details, see [CHANGELOG.md](https://github.com/kbknapp/clap-validators/blob/master/CHANGELOG.md) 46 | //! 47 | //! ## About 48 | //! 49 | //! `clap-validators` are pre-defined functions which can be dropped in to [clap](https://github.com/kbknapp/clap-rs) argument declarations to perform simple value validations. 50 | //! 51 | //! ## Validators 52 | //! 53 | //! Below are the validators which are included with `clap-validators`; full descriptions and usage can be found in the [documentation](http://kbknapp.github.io/clap-validators/clap-validators/index.html) and [examples/](examples) directory 54 | //! 55 | //! * `clap_validators::numeric` 56 | //! * `is_number` - Valid `i64` or `f64` 57 | //! 58 | //! ## Quick Example 59 | //! 60 | //! The following examples show a quick example of using `clap-validators` in combination with `clap` to perform some basic validation of arguments. 61 | //! 62 | //! ```rust 63 | //! extern crate clap; 64 | //! extern crate clap_validators; 65 | //! 66 | //! use clap::{Arg, App}; 67 | //! use clap_validators; 68 | //! 69 | //! fn main() { 70 | //! let matches = App::new("My Super Program") 71 | //! .arg(Arg::with_name("infile") 72 | //! .short("i") 73 | //! .value_name("FILE") 74 | //! .help("Some file to read") 75 | //! .takes_value(true) 76 | //! .validator(clap_validators::fs::is_file)) 77 | //! .get_matches(); 78 | //! 79 | //! // program logic goes here, and we can make the assumption that "infile" is a valid file... 80 | //! } 81 | //! ``` 82 | //! 83 | //! If you were to compile any of the above programs and run them with the flag `-i ` where `` wasn't actually a valid file, one would see the graceful exit of: 84 | //! 85 | //! ```sh 86 | //! $ myprog -i not-a-file 87 | //! error: not-a-file isn't a valid file 88 | //! ``` 89 | //! 90 | //! ## Try it! 91 | //! 92 | //! To test out `clap-validators`: 93 | //! * Create a new cargo project `$ cargo new fake --bin && cd fake` 94 | //! * Add `clap-validators` and `clap` to your `Cargo.toml` 95 | //! * 96 | //! ```toml 97 | //! [dependencies] 98 | //! clap = "2" 99 | //! clap-validators = "0" 100 | //! ``` 101 | //! 102 | //! * Add the following to your `src/main.rs` 103 | //! 104 | //! ```rust 105 | //! extern crate clap; 106 | //! extern crate clap_validators; 107 | //! 108 | //! use clap::{App, Arg}; 109 | //! use clap_validators; 110 | //! 111 | //! fn main() { 112 | //! let m = App::new("fake") 113 | //! .arg(Arg::with_name("infile") 114 | //! .required(true) 115 | //! .validator(clap_validators::fs::is_file)) 116 | //! .get_matches(); 117 | //! 118 | //! println!("success!"); 119 | //! } 120 | //! ``` 121 | //! 122 | //! * Build your program `$ cargo build --release` 123 | //! * Create a file, since we'll be testing if something is a file or not `touch myfile` 124 | //! * Run with help or version `$ ./target/release/fake myfile` 125 | //! * Or to see this fail, run again with something that is not a file, such as a directory `mkdir mydir`, `./target/release/fake mydir` 126 | //! 127 | //! ## Usage 128 | //! 129 | //! For full usage, add `clap-validators` as a dependency in your `Cargo.toml` file to use from crates.io: 130 | //! 131 | //! ```toml 132 | //! [dependencies] 133 | //! clap_validators = "0" 134 | //! ``` 135 | //! 136 | //! Or track the latest on the master branch at github: 137 | //! 138 | //! ```toml 139 | //! [dependencies.clap_validators] 140 | //! git = "https://github.com/kbknapp/clap-validators.git" 141 | //! ``` 142 | //! 143 | //! Add `extern crate clap_validators;` to your crate root. 144 | //! 145 | //! ### More Information 146 | //! 147 | //! You can find complete documentation on the [github-pages site](http://kbknapp.github.io/clap-validators/clap_validators/index.html) for this project. 148 | //! 149 | //! You can also find usage examples in the [examples/](examples) directory of this repo. 150 | //! 151 | //! ## How to Contribute 152 | //! 153 | //! Contributions are always welcome! And there is a multitude of ways in which you can help depending on what you like to do, or are good at. Anything from adding validators, documentation, code cleanup, issue completion, new features, you name it, even filing issues is contributing and greatly appreciated! 154 | //! 155 | //! Another really great way to help is if you find an interesting, or helpful way in which to use `clap-validators`. You can either add it to the [examples/](examples) directory, or file an issue and tell me. I'm all about giving credit where credit is due :) 156 | //! 157 | //! Please read [CONTRIBUTING.md](.github/CONTRIBUTING.md) before you start contributing. 158 | //! 159 | //! ### Running the tests 160 | //! 161 | //! If contributing, you can run the tests as follows (assuming you're in the `clap-validators` directory) 162 | //! 163 | //! ``` 164 | //! $ cargo test 165 | //! 166 | //! # Only on nightly compiler: 167 | //! $ cargo build --features lints 168 | //! ``` 169 | //! 170 | //! ## License 171 | //! 172 | //! `clap-validators` is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses. 173 | //! 174 | //! See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details. 175 | //! 176 | //! ### Deprecations 177 | //! 178 | //! Old method names will be left around for several minor version bumps, or one major version bump. 179 | //! 180 | //! As of 0.0.1: 181 | //! 182 | //! * None! 183 | 184 | #![crate_type= "lib"] 185 | #![cfg_attr(feature = "nightly", feature(plugin))] 186 | #![cfg_attr(feature = "lints", plugin(clippy))] 187 | #![cfg_attr(feature = "lints", deny(warnings))] 188 | #![cfg_attr(not(any(feature = "lints", feature = "nightly")), deny(unstable_features))] 189 | #![deny( 190 | missing_docs, 191 | missing_debug_implementations, 192 | missing_copy_implementations, 193 | trivial_casts, 194 | trivial_numeric_casts, 195 | unused_import_braces, 196 | unused_qualifications)] 197 | 198 | /// Validators which work on the file system, such as determining valid files, links, or 199 | /// directories 200 | pub mod fs; 201 | /// Validators which work on numbers 202 | pub mod num; 203 | -------------------------------------------------------------------------------- /src/num/mod.rs: -------------------------------------------------------------------------------- 1 | /// Ensure valid `u64` or `f64 > 0` 2 | pub fn is_positive(v: String) -> Result<(), String> { 3 | if v.parse::().is_ok() { 4 | return Ok(()); 5 | } else { 6 | if let Ok(f) = v.parse::() { 7 | if f > 0_f64 { 8 | return Ok(()) 9 | } 10 | } 11 | } 12 | Err(format!("{} isn't a positive number", &*v)) 13 | } 14 | --------------------------------------------------------------------------------