Skip to content

Commit

Permalink
Add option to debug with command-line arguments. (leoluz#15)
Browse files Browse the repository at this point in the history
This commit introduces a new option called:

   Debug (Arguments)

When selected, the user will be prompted to enter
any command line arguments to provide to the program
being debugged.

The initial implementation is naive in that it splits
the input string using the space character (" ") as
the separator.
  • Loading branch information
freddiehaddad authored Sep 16, 2022
1 parent fca8bf9 commit c2902bb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ To debug the closest method above the cursor use you can run:

It is better to define a mapping to invoke this command. See the mapping section bellow.

### Debugging with command-line arguments

1. Select the option `Debug (Arguments)`
1. Enter each argument separated by a space (i.e. `option1 option2 option3`)
1. Press enter

![Start Debug Session with Arguments](./images/image1.png "Start Debug Session with Arguments")
![Enter Arguments](./images/image2.png "Enter Arguments")
![Begin Debugging](./images/image3.png "Being Debugging")

## Mappings

```vimL
Expand Down
Binary file added images/image1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/image2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/image3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions lua/dap-go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ local function load_module(module_name)
return module
end

local function get_arguments()
local co = coroutine.running()
if co then
return coroutine.create(function()
local args = {}
vim.ui.input({ prompt = 'Args: ' }, function(input)
args = vim.split(input or "", " ")
end)
coroutine.resume(co, args)
end)
else
local args = {}
vim.ui.input({ prompt = 'Args: ' }, function(input)
args = vim.split(input or "", " ")
end)
return args
end
end

local function setup_go_adapter(dap)
dap.adapters.go = function(callback, config)
local stdout = vim.loop.new_pipe(false)
Expand Down Expand Up @@ -75,6 +94,13 @@ local function setup_go_configuration(dap)
request = "launch",
program = "${file}",
},
{
type = "go",
name = "Debug (Arguments)",
request = "launch",
program = "${file}",
args = get_arguments,
},
{
type = "go",
name = "Debug Package",
Expand Down

0 comments on commit c2902bb

Please sign in to comment.