Skip to content

Commit

Permalink
Adding Arguments with argparse module
Browse files Browse the repository at this point in the history
  • Loading branch information
juba0x00 committed Aug 29, 2022
1 parent b060204 commit a6260a7
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
74 changes: 74 additions & 0 deletions Arguments/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
## Simple argparse
```py
import argparse
```

## Creating Parser
```py
parser = argparse.ArgumentParser(
description='DESCRIPTION: this tool is a very fast tool for fuzzing',
usage='file.py <url> [OPTIONS]',
add_help=True
)

```

## add Positional arguemnt
```py
parser.add_argument('URL', help="Specify the URL")
```


## add argument with another type (Default str), so you can do Math operations
```py
parser.add_argument('-n', metavar='number', help='input a number', type=int)
```

## add long Option
```py
parser.add_argument('--verbose', help="increase output verbosity", action='store_true')
```
## add short Option

```py
parser.add_argument("-v", help="increase output verbosity", action="store_true")
```

## changing verbosity level
```py
parser.add_argument('-V', help='Verbosity Level', action='count', default=0)
```

## add long & short option
```py
parser.add_argument("-hf", '--hidden-files', help="list only hidden files", action="store_false")
```

## Parse arguments
```py
args = parser.parse_args()
```


## Manage conflicts
```py
group = parser.add_mutually_exclusive_group()
group.add_argument("-v", "--verbose", action="store_true")
group.add_argument("-q", "--quiet", action="store_true")
```
![conflict](images/fast-slow)

## show results
```py
print(f'all the arguments: {args}')
print(f'URL: {args.URL}')
print(f'Math Operations: {args.n}^2 = {args.n**2}')
print(f'not Verbosity: {args.verbose}')
print(f'verbose? (long option): {args.verbose}')
print(f'verbose? (short option): {args.v}')
print(f'hidden files: {args.hidden_files}') # note: this is hidden_files not hidden-files like line # note: you can't use the short options if there is long -> can't use args.hf because args.hidden_files exists
print(f'mode: {"fast" if args.fast else "slow"}')
```
![help](images/help)
![test](images/test)

54 changes: 54 additions & 0 deletions Arguments/argparsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Simple argparse

import argparse


# Creating Parser
parser = argparse.ArgumentParser(
description='DESCRIPTION: this tool is a very fast tool for fuzzing',
usage='file.py <url> [OPTIONS]',
add_help=True # Default
)

# add Positional arguemnt
parser.add_argument('URL', help="Specify the URL")


# add argument with another type (Default str), so you can do Math operations
parser.add_argument('-n', metavar='<number>', help='input a number', type=int)


# add long Option
parser.add_argument('--verbose', help="increase output verbosity", action='store_true')


# add short Option
parser.add_argument("-v", help="increase output verbosity", action="store_true")


# changing verbosity level
parser.add_argument('-V', help='Verbosity Level', action='count', default=0)

# add long & short option
parser.add_argument("-hf", '--hidden-files', help="list only hidden files", action="store_false")

# Manage Conflicts

group = parser.add_mutually_exclusive_group()
group.add_argument("--fast", action="store_true")
group.add_argument("--slow", action="store_true")

# Parse arguments
args = parser.parse_args()


print(f'all the arguments: {args}')
print(f'URL: {args.URL}')
print(f'Math Operations: {args.n}^2 = {args.n**2}')
print(f'verbose? (long option): {args.verbose}')
print(f'verbose? (short option): {args.v}')
print(f'Verbosity Level: {args.V}')
print(f'hidden files: {args.hidden_files}') # note: this is hidden_files not hidden-files like line
# note: you can't use the short options if there is long -> can't use args.hf because args.hidden_files exists

print(f'mode: {"fast" if args.fast else "slow"}')
Binary file added Arguments/images/fast-slow
Binary file not shown.
Binary file added Arguments/images/help
Binary file not shown.
Binary file added Arguments/images/test
Binary file not shown.
3 changes: 3 additions & 0 deletions gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__/
test*.py

0 comments on commit a6260a7

Please sign in to comment.