Overview
Argrust is a simple argument parser that can perform Complicated tasks.
Badges
Table of Contents
Installation
can be installed globally using:
or, can be installed just for your project:
Usage
Importing
use ;
Code Description:
-
Arguments
: This is a struct for storing all kinds of info about arguments.- gotargs: stores all arguments that are received throught the command line.
- allargs: stores all arguments that you define.
- arguments: stores the arguments that were received in
gotargs
and are defined inallargs
. - errors: stores errors if any.
- data: this is a vector of custom structs named
ArgumentDescription
. We will come to that soon. For now, just understand that it will store data about the argument if provided. - used: stores arguments that were already used or visited by the user.
-
ArgumentDescription
: This is a struct for describing the arguments. While it is optional to provide data about arguments, A shorter counterpart for the argument is manadatory.- name: stores a name of the argument. ->Optional
- desciption: stores a short description of the argument. -> Optional
- shorter_counterpart: stores a shorter counterpart of the main argumnet. Such as
--abc
is the main argument and-a
is a shorter counterpart. -> Mandatory.
-
FetchTypes
: This is an enum for Fetching values of arguments.-
Fetch(usize): This can be used with the
.fetch()
method where you need to specify how many values u are expecting. Suppose there are two arguments-a
and-b
, and it is passed as -Then, to fetch the values
1
and2
after-a
, you can specify that you are expectingFetch(2)
. Similarly to fetch34
and56
you can useTillLast
to fetch all the arguments till the last of the string. Or if you are not sure, just useFetch(2)
.
-
Defining Arguments and Related
Arguments can be defined using the following way:
let mut args = new;
The line std::env::args().skip(1).collect()
returns a list of arguments passed to the program except the first argument which is the filename.
To add arguments:
let description_argument1 = new
.name // this is optional
.description // this is optional
.short; // this is mandatory
args.add;
To parse the arguments:
args.analyse; // this will fill the Arguments.arguments if any
// argument is received that is defined by you
To check if an argument is received:
// suppose the argument -a was received. you can check it by two ways.
if args.ifarg
// NOTE: the above function will make the above argument as "used".
// Which means the user is already done with it. More Functionalities
// will be added on this later on.
// To avoid marking the argument as used, use:
if args.ifarg_force
To fetch values given to an argument:
// suppose the argument was $ some_program -a 1 2 3
// we are expecting 3 values for this.
let values: = args.fetch;
// NOTE: if we are only expecting 2 values, then the last value
// will just be discarded.
// if the number of arguments expected is not known at the current time,
// you have two options -> FetchTypes::TillNext or FetchTypes::TillLast
// NOTE: if it is not known if the argument u are fetching for is in the
// middle or at the last, it is better to use FetchTypes::TillNext.
// It will try to find if this argument is in the middle or not.
// If it is in the middle, then it will fetch all the values between the
// argument and the next argument.
// And if it finds out that the argument is at the last, it will auto-rerun
// with FetchTypes::TillLast. Pretty Handy right!
// use this like:
let values: = args.fetch;
// NOTE: so if there is no next argument for '--arg', it will run again
// with FetchTypes::TillLast
// Suppose the position of the argument is known to be at the last,
// then simple use FetchTypes::TillLast
// This will fetch all the arguments till Last.
let values: = args.fetch;
To remove an argument from defined arguments:
args.remove; // or args.remove("--arg")
// while defining let mut args = Arguments::new(...);, make sure
// to add "mut" or else, add() and remove() wont work.
To fetch data about arguments:
// get from defined arguments:
// by number
let some_arg = args.get_arg_by_number; // this will return "--arg"
// by index
let some_arg = args.get_arg_by_index; // this will also return "--arg"
// get arg description
let some_arg_description = args.get_arg_description.get;
Example
Let's see a real working code to understand the concept better:
use ;
use env;
NOTE: More features are yet to come.
Testing
-
Auto
To test the features, you can either run the following command in a terminal:
NOTE: this will clone the github repo
d33pster/argrust
will make a new folder namedargrust
wherever the terminal was opened. Additionally it will installgcl
- a rust based cli tool which is an alternative to git clone command. For more info aboutgcl
click [here].ADDITIONAL NOTE: If argrust is added using
cargo add argrust
, this feature will not work. Try manual mode or Install usingcargo install argrust
-
Manual
Run the following in a new terminal: