Skip to content

Commit

Permalink
parse_arguments: Fix bool type (#1056)
Browse files Browse the repository at this point in the history
The parse args set the default argument value behaviour for bools
inconsistenly with the other types: A function had to say 'False' if it
wanted the default to be 'True'. This fixes that.
  • Loading branch information
Grazfather authored Jan 26, 2024
1 parent 570cc03 commit 13a9339
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
8 changes: 4 additions & 4 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ A basic example would be as follow:
class MyCommand(GenericCommand):
[...]

@parse_arguments({"foo": [1,]}, {"--bleh": "", ("--blah", "-l): True})
@parse_arguments({"foo": [1,]}, {"--bleh": "", ("--blah", "-l): False})
def do_invoke(self, argv, *args, **kwargs):
args = kwargs["arguments"]
if args.foo == 1: ...
Expand All @@ -216,9 +216,9 @@ gef➤ mycommand --blah 3 14 159 2653
The function `MyCommand!do_invoke()` can use the command line argument value

```python
args.foo --> [3, 14, 159, 2653] # a List(int) from user input
args.bleh --> "" # the default value
args.blah --> True # set to True because user input declared the option (would have been False otherwise)
args.foo == [3, 14, 159, 2653] # a List(int) from user input
args.bleh == "" # the default value
args.blah == True # set to True because user input declared the option (would have been False otherwise)
```

### Adding new architectures
Expand Down
25 changes: 13 additions & 12 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,10 @@ def wrapper(*args: Any, **kwargs: Any) -> Callable:
argname = [argname,]
if argtype is int:
argtype = int_wrapper
if argtype is bool:
parser.add_argument(*argname, action="store_true" if argvalue else "store_false")
else:
parser.add_argument(*argname, type=argtype, default=argvalue)
elif argtype is bool:
parser.add_argument(*argname, action="store_false" if argvalue else "store_true")
continue
parser.add_argument(*argname, type=argtype, default=argvalue)

parsed_args = parser.parse_args(*(args[1:]))
kwargs["arguments"] = parsed_args
Expand Down Expand Up @@ -4754,7 +4754,7 @@ def format_matrix(self) -> Dict[int, Tuple[str, str, str]]:
}

@only_if_gdb_running
@parse_arguments({"location": "$pc", }, {("--length", "-l"): 256, "--bitlen": 0, "--lang": "py", "--clip": True,})
@parse_arguments({"location": "$pc", }, {("--length", "-l"): 256, "--bitlen": 0, "--lang": "py", "--clip": False,})
def do_invoke(self, _: List[str], **kwargs: Any) -> None:
"""Default value for print-format command."""
args: argparse.Namespace = kwargs["arguments"]
Expand Down Expand Up @@ -6070,7 +6070,7 @@ def __init__(self) -> None:
super().__init__(prefix=False)
return

@parse_arguments({"host": "", "port": 0}, {"--pid": -1, "--qemu-user": True, "--qemu-binary": ""})
@parse_arguments({"host": "", "port": 0}, {"--pid": -1, "--qemu-user": False, "--qemu-binary": ""})
def do_invoke(self, _: List[str], **kwargs: Any) -> None:
if gef.session.remote is not None:
err("You already are in remote session. Close it first before opening a new one...")
Expand Down Expand Up @@ -6172,7 +6172,7 @@ def __init__(self) -> None:
return

@only_if_gdb_running
@parse_arguments({"address": "$pc"}, {"--i": 1, "--b": True, "--f": True, "--n": True})
@parse_arguments({"address": "$pc"}, {"--i": 1, "--b": False, "--f": False, "--n": False})
def do_invoke(self, _: List[str], **kwargs: Any) -> None:
args : argparse.Namespace = kwargs["arguments"]
address = parse_address(args.address)
Expand Down Expand Up @@ -6293,7 +6293,7 @@ def __init__(self) -> None:
return

@only_if_gdb_running
@parse_arguments({"addr": ""}, {"--reset": True})
@parse_arguments({"addr": ""}, {"--reset": False})
def do_invoke(self, _: List[str], **kwargs: Any) -> None:
global gef

Expand Down Expand Up @@ -6349,7 +6349,7 @@ def __init__(self) -> None:
super().__init__(complete=gdb.COMPLETE_LOCATION)
return

@parse_arguments({"address": ""}, {"--allow-unaligned": True, "--number": 1})
@parse_arguments({"address": ""}, {"--allow-unaligned": False, "--number": 1})
@only_if_gdb_running
def do_invoke(self, _: List[str], **kwargs: Any) -> None:
args : argparse.Namespace = kwargs["arguments"]
Expand Down Expand Up @@ -6454,7 +6454,7 @@ def __init__(self) -> None:
self["peek_nb_byte"] = (16, "Hexdump N first byte(s) inside the chunk data (0 to disable)")
return

@parse_arguments({"arena_address": ""}, {("--all", "-a"): True, "--allow-unaligned": True, "--min-size": 0, "--max-size": 0, ("--count", "-n"): -1, ("--summary", "-s"): True, "--resolve": True})
@parse_arguments({"arena_address": ""}, {("--all", "-a"): False, "--allow-unaligned": False, "--min-size": 0, "--max-size": 0, ("--count", "-n"): -1, ("--summary", "-s"): False, "--resolve": False})
@only_if_gdb_running
def do_invoke(self, _: List[str], **kwargs: Any) -> None:
args = kwargs["arguments"]
Expand Down Expand Up @@ -7134,7 +7134,7 @@ def __init__(self) -> None:
self["ps_command"] = (f"{gef.session.constants['ps']} auxww", "`ps` command to get process information")
return

@parse_arguments({"pattern": ""}, {"--attach": True, "--smart-scan": True})
@parse_arguments({"pattern": ""}, {"--attach": False, "--smart-scan": False})
def do_invoke(self, _: List, **kwargs: Any) -> None:
args : argparse.Namespace = kwargs["arguments"]
do_attach = args.attach
Expand Down Expand Up @@ -8195,7 +8195,7 @@ def __init__(self) -> None:
return

@only_if_gdb_running
@parse_arguments({"address": "",}, {("--reverse", "-r"): True, ("--size", "-s"): 0})
@parse_arguments({"address": "",}, {("--reverse", "-r"): False, ("--size", "-s"): 0})
def do_invoke(self, _: List[str], **kwargs: Any) -> None:
valid_formats = ["byte", "word", "dword", "qword"]
if not self.format or self.format not in valid_formats:
Expand Down Expand Up @@ -11077,6 +11077,7 @@ def root(self) -> Optional[pathlib.Path]:
self._root = pathlib.Path(f"/proc/{self.pid}/root")
return self._root


class GefRemoteSessionManager(GefSessionManager):
"""Class for managing remote sessions with GEF. It will create a temporary environment
designed to clone the remote one."""
Expand Down

0 comments on commit 13a9339

Please sign in to comment.