donnieparka

donnieparka

Can't run IO.inspect(args) in the first main(args) line

defmodule Createproj do
  @moduledoc """
  Documentation for `Createproj`.
  """
  def print_help do
    IO.puts(
      "Usage: newP" <>
        " -f filepath: creates a new project\n" <>
        "-b: adds boilerplate\n"
    )
  end

  def write_boilerplate(project_name) do
    file_name = project_name <> ".ex"
    file_path = Path.join([project_name, file_name])
    boilerplate = "defmodule #{project_name} do\nend"
    File.write(file_path, boilerplate, [:append])
  end

  def get_filename(args) do
    Enum.with_index(args)
    |> Enum.find(fn {arg, index} -> arg == "-f" and index < length(args) - 1 end)
    |> case do
      {_, index} ->
        Enum.at(args, index + 1)

      {nil} ->
        nil
    end
  end

  def create_folder(project_name, file_name \\ "") do
    folder_path = Path.join([project_name, file_name])

    File.mkdir(folder_path)
    |> case do
      :ok -> IO.puts("created#{folder_path}")
      {_, _} -> IO.puts("failed to make #{file_name}")
    end
  end

  def main(args) do
    IO.inspect(args)
    new_file = Enum.member?(args, "-f")
    boilerplate = Enum.member?(args, "-b")

    cond do
      new_file and not boilerplate ->
        project_name = get_filename(args)
        create_folder(project_name)
        create_folder(project_name, "/lib")
        create_folder(project_name, "/test")

      new_file and boilerplate ->
        project_name = get_filename(args)
        create_folder(project_name)
        create_folder(project_name, "/lib")
        create_folder(project_name, "/test")
        write_boilerplate(project_name)

      true ->
        print_help()
    end
  end
end

compilation works great but when I run:

~/projects ./createproj -f dio -b
** (MatchError) no match of right hand side value: "dio"
    (createproj 0.1.0) lib/createproj.ex:33: Createproj.main/1
    (elixir 1.18.1) lib/kernel/cli.ex:137: anonymous fn/3 in Kernel.CLI.exec_fun/2

If I run it from the iex like this:

Createproj.main(["-f", "cane", "-b"])
["-f", "cane", "-b"]
createdcane
createdcane/lib
createdcane/test
:ok

as you can see everything works… any idea what’s going on?

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

If this is an escript, you need to rebuild the escript after each change you make.

Also Liked

al2o3cr

al2o3cr

Line 33 of what you posted isn’t in main and couldn’t raise a MatchError anyways:

folder_path = Path.join([project_name, file_name])

My guess is that a compiled version of Createproj is running, confusing the situation.

Eiji

Eiji

I have tried this in newly generated project. I have only added escript: [main_module: Createproj] to Keyword returned in project function inside mix.exs and it works as expected. We would need something more to see where is the problem as for now I cannot reproduce it.

al2o3cr

al2o3cr

The BEAM caches compiled bytecode to disk when it reads .ex files, so rebooting will not make a difference. Usually the advice is to remove the _build folder, but I’m not sure where that lives for escripts.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement