Skip to content

Encapsulate the argument parsing logic in a class #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 8, 2017
Merged

Encapsulate the argument parsing logic in a class #64

merged 9 commits into from
Oct 8, 2017

Conversation

diogoosorio
Copy link
Contributor

This PR refactors the way the CLI argument parsing is done. It contains the argument definition and parsing logic within the cli_argument_parser class.

The rationale for doing this is threefold:

  1. Have the logic isolated enough so that it can be tested.
  2. In the near future, centralize how the CLI argument should be translated into a "scan request"
  3. Give the first steps into decoupling the CLI logic from the core module (i.e. be able to have a web-based UI for the virtual_host_scanner).

I'm striving for an API something like the following:

# argument parsing
args = CliArgumentParser().parse(argv[1:])
args.scanner.wordlist # yields an iterable datastructure with all the words, the parser holds the logic for how this list is built
args.scanner.ignore_http_codes # yields a list with all the to be ignored http status code
...

# scanning logic
logger = CliLogger(level=logging.DEBUG) # outputs to stdout/stderr
scanner = VHostScanner(logger=logger)
result = scanner.scan(**request) # instance of ScannerResult

# scanning result serialization
serialized_result = result.serialize(format=args.serializer.format) # instance of a ResultJsonSerializer/ResultPlainSerializer
serialized_result.save(file=args.serializer.destination) # flush the serialized result into a file
str(serialized_result) # the json/text serialized string

This is definitely an opinionated PR and as such I'm obviously 100% open to discussion.

diogoosorio and others added 8 commits October 8, 2017 17:58
This function will be responsible from parsing the argv argument list
and build an object which encapsulates all the scan command parameters.
The lib directory was being listed as a git ignore directory - this
directory contains the main project files.
This class serves as an indirection layer over the argparser library and
encapsulates the CLI command definition and the translation of the raw
user input into the known set of arguments.

The ideia in the near future is for this class to be able to pre-process
all the CLI input into some sort of request object for the scanner, in
order to decouple this task from the main scanner function which
currently is one of the reasons that prevents the function to be used in
any other context besides a CLI run.
Instead of build the argparser.ArgumentParser directly, delegate to the
new scanner_argument_parser class the responsibility of parsing the CLI
arguments.
Added first-hit back into the codebase as it was added into the application after this pr.
@codingo codingo merged commit 515cb06 into codingo:master Oct 8, 2017
@codingo
Copy link
Owner

codingo commented Oct 8, 2017

@diogoosorio I agree. I had to make a few very minor changes due to an earlier PR but everything is now merged. Can definitely see this keeping a more manageable code base as we bring more features into the project - and agree with the API approach, makes a lot of sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants